我可以设置一个单元测试来运行多次并返回成功的百分比吗? [英] Can I set up a unit test to run many times and return the percentage success?

查看:19
本文介绍了我可以设置一个单元测试来运行多次并返回成功的百分比吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试的新手,一直在网上搜索,试图找出如何进一步自动化我的单元测试.我正在构建一个注册方案,我想做的是用各种硬盘序列号、应用程序号等对其进行测试.我想生成一个注册密钥,然后检查它以确保它正确解码.我希望使用各种输入自动运行测试(使用集成的 Visual Studio 测试环境),经过数千次运行后,找出测试成功和失败的百分比.这可能吗?下面是我的测试方法:

I am new to unit testing, and have been scouring the web, trying to figure out how to further automate my unit tests. I am building a registration scheme, and what I want to do is test it with a variety of hard drive serial numbers, app numbers, etc. I want to generate a registration key and then check it to make sure it decodes properly. I am looking to automate running the test (with the integrated Visual Studio Test Environment) with a variety of inputs, and after thousands of runs, find out what % of the tests were successful and unsuccessful. Is this possible? Below is my test method:

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

推荐答案

就我个人而言,我发现向该方法抛出大量随机数据并确保它一切正常"方法的价值很小.

Personally, I find very little value in the "throw lots of random data at the method and make sure it all works" approach.

如果您为该方法提供 500 个不同的序列号并且它们都有效,那很好.但是他们在您的代码中测试了哪些特定场景?如果您不能回答这个问题,那么您可能是在重复测试场景,更重要的是,缺少测试场景.

If you give 500 different serial numbers to the method and they all work, that's fine. But what specific scenarios are they testing in your code? If you can't answer that question, you're probably duplicating test scenarios, and more importantly, missing test scenarios.

与其将测试用例扔在墙上,看看哪些是有效的,不如分析您的代码并确定关键的成功和失败标准,并制定执行这些标准的测试.这有一个附带的好处,即让您的测试更加冗长,并让您的团队成员仅通过阅读测试名称就可以更好地了解代码应该做什么.您的测试应该命名为描述它们正在测试的什么,而不是 GeneratingValidKeyTest.

Instead of throwing test cases at the wall and seeing what sticks, analyze your code and identify the critical success and failure criteria and craft tests that exercise those criteria. This has a side benefit of making your tests more verbose and giving your team members a better idea of what the code is supposed to be doing just by reading the test names. Instead of GeneratingValidKeyTest, your tests should be named such that they describe what they're testing.

举个例子,假设您正在构建一个计算器.用你的方法,你会抛出大量的加法案例——1+1、1+3、5+30 等等.但你很可能会错过 1+Int32.MaxValue.或者也许您不会尝试添加负数.或者测试如果输入不是有效数字会发生什么.等等.

As an example, let's say you're building a calculator. With your approach, you'd toss a ton of addition cases at it -- 1+1, 1+3, 5+30, etc. But it's likely that you'd miss 1+Int32.MaxValue. Or maybe you wouldn't try adding negative numbers. Or testing what happens if the input isn't a valid number. And so on.

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

Good tests force you to think through all of these types of scenarios as you're writing them.

这篇关于我可以设置一个单元测试来运行多次并返回成功的百分比吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆