測試環境搭建
鏈接:使用 dotnet test 和 xUnit 在 .NET 中對 C# 進行單元測試
實用技巧
忽略測試方法
忽略整片的
-
忽略 Test1 至 Test2 之間的全部測試
public class Program_Test { #if false [Fact] public void Test1() {...} [Theory] [InlineData(...)] public void Test2(...) {...} #endif [Fact] public void Test3() {...} }
忽略指定的
-
忽略 Test1 和 Test3 兩個測試
public class Program_Test { // 將值設為 null 會解除忽略 const skip = "忽略原因"; [Fact] public void Test1() {...} [Theory(Skip = skip)] [InlineData(...)] public void Test2(...) {...} [Fact(Skip = skip)] public void Test3() {...} }
打印測試方法中的輸出
- 此文(鏈接)中的“單元測試中如何輸出日誌”部分有説明
問題排除
新增的方法無法測試
執行“Run Tests in Current File”(運行文件中的測試)
提示“No tests found in this file”(沒找到任何測試),左邊也沒有測試的操作圖標
解決辦法:需要刷新一下全部測試(下面方法選一個)
- 命令行中執行
dotnet test
- 測試面板中點擊
Refresh Tests按鈕
新增的方法就會出現在測試列表中,辣個圖標也出現了
參考鏈接
- 使用 dotnet test 和 xUnit 在 .NET 中對 C# 進行單元測試
- 如何在vscode中為c#編寫單元測試
- C# 單元測試