我可以设置单元测试多次运行并返回成功百分比吗?

时间:2014-11-11 16:36:13

标签: c# visual-studio 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");
    }

4 个答案:

答案 0 :(得分:2)

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

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

但是,你不应该找到一定比例的测试通过。单元测试的目的是确保所有测试场景都通过。

采用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"
};

答案 1 :(得分:2)

就个人而言,我发现在该方法中抛出大量随机数据并确保其全部有效并且#34;做法。

如果您为方法提供500个不同的序列号并且它们都有效,那很好。但是他们在您的代码中测试了哪些具体方案?如果您无法回答该问题,则可能会重复测试方案,更重要的是,缺少测试方案。

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

举个例子,让我们说你正在建立一个计算器。通过你的方法,你可以在它上面添加大量的附加案例 - 1 + 1,1 + 3,5 + 30等。但是你可能会错过1+ { {1}}。或者也许你不会尝试添加负数。或测试如果输入不是有效数字会发生什么。等等。

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

答案 2 :(得分:1)

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" }
};

答案 3 :(得分:1)

You can use Microsoft's Unit Test Framework and make it read test data from a data source. The advantage of using MSTest is that it'll run on the Express Editions of Visual Studio.

You won't get a percentage of errors though, and I agree with @DanielMann, instead you have to ensure that your tests cover all possibilities, and that they all pass.

So, considering you have done that and now you have a list of cases to test, you can use the code below. It uses the 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"
        );
    }
}

In the Test Explorer window you'll get an output like this (failed tests are shown first):

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: