我可以设置unit testing多次运行并返回成功百分比吗?

我是unit testing的新手,并且一直在搜索网络,试图弄清楚如何进一步自动化我的unit testing。 我正在建立一个注册方案,我想要做的是用各种硬盘驱动器序列号,应用程序编号等测试它。我想生成一个注册密钥,然后检查它以确保它正确解码。 我希望通过各种输入自动运行测试(使用集成的Visual Studio测试环境),经过数千次运行后,找出成功和不成功的测试百分比。 这可能吗? 以下是我的测试方法:

[TestMethod] public void GeneratingValidKeyTest() { int numReadDevices; string appNum = "123"; string hddSerial = "1234567890"; string numDevices = "12"; string regNumber = Registration.GenerateKey(appNum, numDevices, hddSerial); Assert.IsTrue(Registration.CheckKey(regNumber, appNum, out numReadDevices, hddSerial), "Generated key does not pass check."); Assert.AreEqual(int.Parse(numDevices), numReadDevices,"Number of registered devices does not match requested number"); } 

如果您使用NUnit,您可以设置一系列ValueSource来提供给您的方法。

如果您有appNum,hddSerial和numDevices的单独值源,您将获得appNum * hddSerial * numDevices测试次数。

但是,您不应该想要找到一定比例的测试通过。 unit testing的目的是确保所有测试场景都通过。

以Max为例,将其作为ValueSources:

 [Test] public void DivideTest([ValueSource("AppNums")]string appNum, [ValueSource("Serials")]string hddSerial, [ValueSource("NumDevices")]string numDevices) { string regNumber = Registration.GenerateKey(appNum, numDevices, hddSerial); Assert.IsTrue(Registration.CheckKey(regNumber, appNum, out numReadDevices, hddSerial), "Generated key does not pass check."); Assert.AreEqual(int.Parse(numDevices), numReadDevices,"Number of registered devices does not match requested number"); } static object[] AppNums = { "1", "2", "3", "4", "5", "6", "7" }; static object[] Serials = { "EXAMPLE", "ANOTHER", "AND AGAIN" }; static object[] NumDevices = { "1", "2", "3", "4", "5", "6", "7" }; 

就个人而言,我发现“在方法中抛出大量随机数据并确保一切正常”的方法很少。

如果你给方法提供500个不同的序列号,它们都可以工作,那很好。 但是他们在您的代码中测试了哪些具体方案? 如果您无法回答这个问题,那么您可能会复制测试场景,更重要的是, 缺少测试场景。

而不是将测试用例扔到墙上,看看有什么粘贴,分析您的代码并确定关键的成功和失败标准,并制定符合这些标准的测试。 这样做的另一个好处是可以让您的测试更加冗长,并通过阅读测试名称让您的团队成员更好地了解代码应该做什么。 而不是GeneratingValidKeyTest ,您的测试应该被命名为描述他们正在测试的内容。

举个例子,假设你正在构建一个计算器。 使用你的方法,你会在它上面添加大量的附加案例 – 1 + 1,1 + 3,5 + 30等。但是你可能会错过1+ Int32.MaxValue 。 或者也许你不会尝试添加负数。 或测试输入不是有效数字时会发生什么。 等等。

好的测试会迫使您在编写时考虑所有这些类型的场景。

在NUnit中它看起来像

 [Test, TestCaseSource("DivideCases")] public void DivideTest(string appNum, string hddSerial, string hddSerial) { string regNumber = Registration.GenerateKey(appNum, numDevices, hddSerial); Assert.IsTrue(Registration.CheckKey(regNumber, appNum, out numReadDevices, hddSerial), "Generated key does not pass check."); Assert.AreEqual(int.Parse(numDevices), numReadDevices,"Number of registered devices does not match requested number"); } static object[] DivideCases = { new object[] { "123", "3", "4" }, new object[] { "12223", "35", "54" }, new object[] { "12123123", "23", "14" } }; 

您可以使用Microsoft的unit testing框架,并使其从数据源读取测试数据。 使用MSTest的优点是它可以在Visual Studio的Express Edition上运行。

你不会得到一定比例的错误,我同意@DanielMann,相反,你必须确保你的测试涵盖所有可能性,并且它们都通过了。

所以,考虑到你已经这样做了,现在你有一个要测试的案例列表,你可以使用下面的代码。 它使用DataSourceAttribute

 [TestClass] public class RegistrationTests { public TestContext TestContext { get; set; } [TestMethod] [DataSource( "System.Data.OleDb", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\MySolution\Serial numbers.mdb""", "Serials", DataAccessMethod.Sequential )] public void GeneratingValidKeyTest() { // Arrange int numReadDevices; var appNum = TestContext.DataRow["appNum"].ToString(); var hddSerial = TestContext.DataRow["hddSerial"].ToString(); var numDevices = TestContext.DataRow["numDevices"].ToString(); // Act var regNumber = Registration.GenerateKey(appNum, numDevices, hddSerial); // Assert Assert.IsTrue( Registration.CheckKey(regNumber, appNum, out numReadDevices, hddSerial), "Generated key does not pass check." ); Assert.AreEqual( int.Parse(numDevices), numReadDevices, "Number of registered devices does not match requested number" ); } } 

在Test Explorer窗口中,您将获得这样的输出(首先显示失败的测试):

 Test Name: GeneratingValidKeyTest Test FullName: MySolution.UnitTest.RegistrationTests.GeneratingValidKeyTest Test Source: C:\MySolution\MySolution.UnitTest\RegistrationTests.cs : line 15 Test Outcome: Failed Test Duration: 0:00:00,017832 Result1 Name: GeneratingValidKeyTest (Data Row 1) Result1 Outcome: Failed Result1 Duration: 0:00:00,0082728 Result1 StackTrace: at MySolution.UnitTest.RegistrationTests.GeneratingValidKeyTest() in C:\MySolution\MySolution.UnitTest\RegistrationTests.cs:line 27 Result1 Message: Assert.AreEqual failed. Expected:<11>. Actual:<12>. Number of registered devices does not match requested number Result3 Name: GeneratingValidKeyTest (Data Row 0) Result3 Outcome: Passed Result3 Duration: 0:00:00,0089332 Result3 StackTrace: Result3 Message: