自定义多输入测试
[DataTestMethod]
[DataRow(-1)]
[DataRow(0)]
[DataRow(1)]
public void ReturnFalseGivenValuesLessThan2(int value)
{
var result = _primeService.IsPrime(value);
Assert.IsFalse(result, $"{value} should not be prime");
}
dotnet test 相关指令
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MSTestNamespace
{
[TestClass]
public class UnitTest1
{
[TestCategory("CategoryA")]
[Priority(1)]
[TestMethod]
public void TestMethod1()
{
}
[Priority(2)]
[TestMethod]
public void TestMethod2()
{
}
}
}
表达式 | 结果 |
---|---|
dotnet test --filter Method | 运行 FullyQualifiedName 包含 Method 的测试。 在 vstest 15.1+ 中可用。 |
dotnet test --filter Name~TestMethod1 | 运行名称包含 TestMethod1 的测试。 |
dotnet test --filter ClassName=MSTestNamespace.UnitTest1 | 运行属于类 MSTestNamespace.UnitTest1 的测试。注意: 由于 ClassName 值应有命名空间,因此 ClassName=UnitTest1 无效。 |
dotnet test --filter FullyQualifiedName!=MSTestNamespace.UnitTest1.TestMethod1 | 运行除 MSTestNamespace.UnitTest1.TestMethod1 之外的其他所有测试。 |
dotnet test --filter TestCategory=CategoryA | 运行含 [TestCategory(“CategoryA”)] 批注的测试。 |
dotnet test --filter Priority=2 | 运行含 [Priority(2)] 批注的测试。 |
使用条件运算符 | 和 &
表达式 | 结果 |
---|---|
dotnet test --filter “FullyQualifiedName~UnitTest1 or TestCategory=CategoryA” | 运行 FullyQualifiedName 包含 UnitTest1 或 TestCategory 是 CategoryA 的测试。 |
dotnet test --filter “FullyQualifiedName~UnitTest1&TestCategory=CategoryA” | 运行 FullyQualifiedName 包含 UnitTest1 且 TestCategory 是 CategoryA 的测试。 |
dotnet test --filter “(FullyQualifiedName~UnitTest1&TestCategory=CategoryA) or Priority=1” | 运行 FullyQualifiedName 包含 UnitTest1 且 TestCategory 是 CategoryA 或 Priority 是 1 的测试。 |
测试已发布的文件:
dotnet vstest out/MyProject.Tests.dll
果你的应用以 netcoreapp 之外的框架为目标,则仍然可以通过使用框架标志传入目标框架来运行 dotnet vstest 命令。
dotnet vstest <MyPublishedTests>.dll --Framework:".NETFramework,Version=v4.6"