❯ dotnet test Determining projects to restore... Restored D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService\PrimeService.csproj (in41 ms). Restored D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\PrimeService.Tests.csproj (in140 ms). PrimeService -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService\bin\Debug\net8.0\PrimeService.dll PrimeService.Tests -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll Test run for D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll (.NETCoreApp,Version=v8.0) Microsoft (R) Test Execution Command Line Tool Version 17.9.0 (x64) Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait... A total of 1 test files matched the specified pattern.
❯ dotnet test Determining projects to restore... All projects are up-to-datefor restore. PrimeService -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService\bin\Debug\net8.0\PrimeService.dll PrimeService.Tests -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll Test run for D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll (.NETCoreApp,Version=v8.0) Microsoft (R) Test Execution Command Line Tool Version 17.9.0 (x64) Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait... A total of 1 test files matched the specified pattern. Failed IsPrime_InputIs1_ReturnFalse [12ms] Error Message: System.NotImplementedException : Please create a test first. Stack Trace: at PrimeService.PrimeService.IsPrime(Int32 candidate) in D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService\PrimeService.cs:line 5 at PrimeService.Tests.PrimeServiceIsPrimeShould.IsPrime_InputIs1_ReturnFalse() in D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\PrimeServiceIsPrimeShould.cs:line 14 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
publicboolIsPrime(int candidate) { if (candidate < 2) { returnfalse; }
thrownew NotImplementedException("Please create a test first."); }
此时再次运行 dotnet test 命令,会发现测试通过:
1 2 3 4 5 6 7 8 9 10 11 12 13
❯ dotnet test Determining projects to restore... All projects are up-to-datefor restore. PrimeService -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService\bin\Debug\net8.0\PrimeService.dll PrimeService.Tests -> D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll Test run for D:\Github\.Net-Samples\UnitTestingUsingNUnit\PrimeService.Tests\bin\Debug\net8.0\PrimeService.Tests.dll (.NETCoreApp,Version=v8.0) Microsoft (R) Test Execution Command Line Tool Version 17.9.0 (x64) Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait... A total of 1 test files matched the specified pattern.