MSTestEnhancer 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MSTestEnhancer --version 1.1.0                
NuGet\Install-Package MSTestEnhancer -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MSTestEnhancer" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MSTestEnhancer --version 1.1.0                
#r "nuget: MSTestEnhancer, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MSTestEnhancer as a Cake Addin
#addin nuget:?package=MSTestEnhancer&version=1.1.0

// Install MSTestEnhancer as a Cake Tool
#tool nuget:?package=MSTestEnhancer&version=1.1.0                

MSTest Enhancer

在 MSTestEnhancer 的帮助下,单元测试将更加容易编写、理解和维护。因为它让单元测试方法和被测类本身之间的关系更加紧密。

如何开始

  1. 在单元测试项目中安装 NuGet 包:MSTestEnhancer
  2. 现在就可以开始编写下文那种风格的单元测试代码了。

推荐的单元测试编写方式

[TestClass]
public class 被测类名Test
{
    [ContractTestCase]
    public void 被测方法名()
    {
        "契约 1(当 Xxx 时,应该发生 Yyy)".Test(() =>
        {
            // 测试用例代码
        });
        
        "契约 2(但当 Zzz 时,应该发生 Www)".Test(() =>
        {
            // 测试用例代码
        });
    }
}

注意到单元测试类的名称和原本的类名称是一一对应的,方法名和原本的方法名是一模一样的;于是,我们不再需要为任何一个单元测试思考命名问题。

单元测试结果页

参数化的单元测试

有些契约需要更多的值组合来验证正确性,那么可以在契约测试用例的后面添加参数。

"质数".Test((int num) =>
{
    // 测试用例代码
}).WithArguments(2, 3, 5, 7, 11);

"{0} 不是质数".Test((int num) =>
{
    // 测试用例代码
}).WithArguments(1, 4);

也可以添加多个参数(最多支持 8 个):

"契约 1,参数中可以带 {0} 和 {1}。".Test((int a, int b) =>
{
    // 现在,a 会等于 2,b 会等于 3。
}).WithArguments(2, 3);

"契约 2".Test((int a, int b) =>
{
    // 现在有两组代码,一组 a=2, b=3;另一组 a=10, b=20。
    // 当然也可以传入元组数组。
}).WithArguments((2, 3), (10, 20));

在显示单元测试结果时,如果契约字符串中含有格式化占位符 {0}{1} 等,会被自动替换为参数的值。

异步的单元测试

Test 方法中传入的每个 Action 都支持 async 关键字,并会在执行测试用例时等待异步操作结束。

额外的黑科技

MSTest v2 支持嵌套类型的单元测试。也就是说,我们可以利用这一点做出近乎无限层级的单元测试树出来。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MSTestEnhancer:

Package Downloads
dotnetCampus.UITest.WPF

The UITest framework for WPF

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on MSTestEnhancer:

Repository Stars
lindexi/lindexi_gd
博客用到的代码
dotnet-campus/AsyncWorkerCollection
高性能的多线程异步工具库。A collection of tools that support asynchronous methods and support high-performance multithreading.
Version Downloads Last updated
2.2.1 1,281 10/27/2022
2.2.1-alpha01 2,752 2/21/2022
2.2.0 5,882 1/6/2022
2.1.2-alpha03 205 12/27/2021
2.1.2-alpha02 189 12/27/2021
2.1.2-alpha01 164 12/27/2021
2.1.1 1,302 8/4/2021
2.1.1-alpha02 220 8/4/2021
2.1.1-alpha01 267 8/4/2021
2.1.0 3,774 3/4/2021
2.0.2-alpha03 130 8/10/2023
2.0.2-alpha02 156 4/18/2023
2.0.1 4,788 5/24/2020
1.6.0 3,152 6/21/2018
1.4.0 1,307 3/20/2018
1.3.0 946 2/26/2018
1.2.1 913 2/24/2018
1.2.0 903 2/13/2018
1.1.0 924 2/12/2018
1.0.0 938 2/11/2018

1. All xml comments are now in English.
2. Add support for .NET Framework 4.5. Now netstandard/net45/net47 are all supported.