tjj*_*son 100 nunit mstest rowtest testcase
我发现TestCaseNUnit中的功能非常有用,可以快速指定测试参数,而无需为每个测试使用单独的方法.MSTest中有类似的东西吗?
[TestFixture]
public class StringFormatUtilsTest
{
[TestCase("tttt", "")]
[TestCase("", "")]
[TestCase("t3a4b5", "345")]
[TestCase("3&5*", "35")]
[TestCase("123", "123")]
public void StripNonNumeric(string before, string expected)
{
string actual = FormatUtils.StripNonNumeric(before);
Assert.AreEqual(expected, actual);
}
}
Run Code Online (Sandbox Code Playgroud)
khl*_*hlr 49
如果不必坚持使用MSTest而您只是使用它来通过Test Explorer运行测试 因为您只有Visual Studio Express版本那么这可能是你的解决方案:
还有的VsTestAdapter VSIX扩展为能够通过测试资源管理器来运行NUnit的测试.不幸的是,VS Express用户无法安装扩展......但幸运的是,VsTestAdapter也带有一个简单的NuGet-Package!
因此,如果您是VS Express用户,只需安装VsTestAdapter NuGet-Package并享受通过Test Explorer运行NUnit测试/测试用例的乐趣!
不幸的是,上述陈述并非如此.虽然完全可以通过Express版本安装软件包,但它没用,因为它无法使用Test Explorer.以前有关于旧版 TestAdapter的附注,该版本已从2.0.0的描述页面中删除:
请注意,它不适用于VS Express
更新:
微软最近宣布推出"MSTest V2"(参见博客文章).这允许您始终如一地(桌面,UWP,...)使用DataRow-attribute!
[TestClass]
public class StringFormatUtilsTest
{
[DataTestMethod]
[DataRow("tttt", "")]
[DataRow("", "")]
[DataRow("t3a4b5", "345")]
[DataRow("3&5*", "35")]
[DataRow("123", "123")]
public void StripNonNumeric(string before, string expected)
{
string actual = FormatUtils.StripNonNumeric(before);
Assert.AreEqual(expected, actual);
}
}
Run Code Online (Sandbox Code Playgroud)
同样,Visual Studio Express的测试资源管理器遗憾地无法识别这些测试.但至少"完整"VS版本现在支持该功能!
要使用它,只需安装NuGet包MSTest.TestFramework和MSTest.TestAdapter(两者都是预发布版).
Thw*_*esy 34
我知道这是一个迟到的答案,但希望它可以帮助其他人.
我到处寻找一个优雅的解决方案,最后自己写了一个.我们在20多个项目中使用它,进行了数千次单元测试和数十万次迭代.永远不会错过一个节拍.
https://github.com/Thwaitesy/MSTestHacks
1)安装NuGet包.
2)从TestBase继承您的测试类
public class UnitTest1 : TestBase
{ }
Run Code Online (Sandbox Code Playgroud)
3)创建一个返回IEnumerable的Property,Field或Method
[TestClass]
public class UnitTest1 : TestBase
{
private IEnumerable<int> Stuff
{
get
{
//This could do anything, get a dynamic list from anywhere....
return new List<int> { 1, 2, 3 };
}
}
}
Run Code Online (Sandbox Code Playgroud)
4)将MSTest DataSource属性添加到测试方法,指向上面的IEnumerable名称.这需要完全合格.
[TestMethod]
[DataSource("Namespace.UnitTest1.Stuff")]
public void TestMethod1()
{
var number = this.TestContext.GetRuntimeDataSourceObject<int>();
Assert.IsNotNull(number);
}
Run Code Online (Sandbox Code Playgroud)
最终结果: 3次迭代就像普通的DataSource :)
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSTestHacks;
namespace Namespace
{
[TestClass]
public class UnitTest1 : TestBase
{
private IEnumerable<int> Stuff
{
get
{
//This could do anything, get a dynamic list from anywhere....
return new List<int> { 1, 2, 3 };
}
}
[TestMethod]
[DataSource("Namespace.UnitTest1.Stuff")]
public void TestMethod1()
{
var number = this.TestContext.GetRuntimeDataSourceObject<int>();
Assert.IsNotNull(number);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Gar*_*Ray 13
我知道这是另一个迟到的答案,但在我的团队被锁定使用MS Test框架时,我们开发了一种技术,它只依赖于匿名类型来保存测试数据数组,并且LINQ循环并测试每一行.它不需要额外的类或框架,并且往往相当容易阅读和理解.它比使用外部文件或连接数据库的数据驱动测试更容易实现.
例如,假设你有一个像这样的扩展方法:
public static class Extensions
{
/// <summary>
/// Get the Qtr with optional offset to add or subtract quarters
/// </summary>
public static int GetQuarterNumber(this DateTime parmDate, int offset = 0)
{
return (int)Math.Ceiling(parmDate.AddMonths(offset * 3).Month / 3m);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用和匿名类型的数组组合到LINQ来编写这样的测试:
[TestMethod]
public void MonthReturnsProperQuarterWithOffset()
{
// Arrange
var values = new[] {
new { inputDate = new DateTime(2013, 1, 1), offset = 1, expectedQuarter = 2},
new { inputDate = new DateTime(2013, 1, 1), offset = -1, expectedQuarter = 4},
new { inputDate = new DateTime(2013, 4, 1), offset = 1, expectedQuarter = 3},
new { inputDate = new DateTime(2013, 4, 1), offset = -1, expectedQuarter = 1},
new { inputDate = new DateTime(2013, 7, 1), offset = 1, expectedQuarter = 4},
new { inputDate = new DateTime(2013, 7, 1), offset = -1, expectedQuarter = 2},
new { inputDate = new DateTime(2013, 10, 1), offset = 1, expectedQuarter = 1},
new { inputDate = new DateTime(2013, 10, 1), offset = -1, expectedQuarter = 3}
// Could add as many rows as you want, or extract to a private method that
// builds the array of data
};
values.ToList().ForEach(val =>
{
// Act
int actualQuarter = val.inputDate.GetQuarterNumber(val.offset);
// Assert
Assert.AreEqual(val.expectedQuarter, actualQuarter,
"Failed for inputDate={0}, offset={1} and expectedQuarter={2}.", val.inputDate, val.offset, val.expectedQuarter);
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用此技术时,使用包含Assert中输入数据的格式化消息有助于您确定哪一行导致测试失败.
我在AgileCoder.net上发布了有关此解决方案的更多背景和详细信息.
Khlr给出了很好的详细解释,显然这种方法开始在VS2015 Express for Desktop中运行.我试图留下评论,但我缺乏声誉不允许我这样做.
让我在这里复制解决方案:
[TestClass]
public class StringFormatUtilsTest
{
[TestMethod]
[DataRow("tttt", "")]
[DataRow("", "")]
[DataRow("t3a4b5", "345")]
[DataRow("3&amp;5*", "35")]
[DataRow("123", "123")]
public void StripNonNumeric(string before, string expected)
{
string actual = FormatUtils.StripNonNumeric(before);
Assert.AreEqual(expected, actual);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,只需安装NuGet包MSTest.TestFramework和MSTest.TestAdapter.
一个问题是
错误CS0433类型'TestClassAttribute'存在于'Microsoft.VisualStudio.QualityTools.UnitTestFramework,Version = 10.0.0.0和'Microsoft.VisualStudio.TestPlatform.TestFramework,Version = 14.0.0.0
因此,请从项目的引用中删除Microsoft.VisualStudio.QualityTools.UnitTestFramework.
非常欢迎您编辑原始回复并删除此回复.
| 归档时间: |
|
| 查看次数: |
38147 次 |
| 最近记录: |