简体   繁体   English

单元测试代码涵盖范围应包括哪些内容?

[英]What should include in unit test code coverage?

I am writing test methods for a class 我正在为一个类编写测试方法

Eg: 例如:

class employee{

   public int ButtonID
        {
            get
            {
                return 
                function(GetValue("ButtonID"), 0);
            }
            set
            {
                SetValue("ButtonID", value);
            }
        }

    public function getEmployeeID()
    {somthing; }

    private function setEmployeeID()
    {somthing; }

    protected button_click(e)
    { somthing; }

}

Here when we calculate code coverage it also includes 在这里,当我们计算代码覆盖率时,它还包括

  1. getters and setter methods 吸气剂和塞特方法
  2. button click events. 按钮单击事件。
  3. public functions 公共职能
  4. private functions 私人职能

Am I supposed to see the coverage of all above four? 我是否应该看到以上四个方面的报道?

Specific to points 1 and 2: 特定于第1点和第2点:

Testing getters and setters depends upon what those accessor methods are doing. 测试getter和setter取决于那些访问器方法在做什么。 If they are auto-implemented, then I do not recommend explicitly testing them. 如果它们是自动实现的,那么我建议您不要显式测试它们。 I trust the .NET Framework's implementation. 我相信.NET Framework的实现。 If you are looking for increased code coverage, then be sure you both get and set the property during other tests, and the coverage will be counted. 如果您希望增加代码覆盖率,请确保在其他测试期间都getset了该属性,并且覆盖率将被计算在内。

If there is something more complicated going on in the property accessors, then it may be useful to have unit tests to help create it (if following test-first TDD) or to ensure the logic is implemented correctly. 如果属性访问器中发生了更复杂的事情,那么进行单元测试来帮助创建它(如果遵循测试优先TDD)或确保逻辑正确实现可能会很有用。

When it comes to UI-specific code, it is not worth trying to use unit testing to test it. 对于特定于UI的代码,不值得尝试使用单元测试来对其进行测试。 Trying to manipulate a UI within a unit test leads to flaky, slow tests. 尝试在单元测试中操作UI会导致不稳定的缓慢测试。 If you want to test something within the button click handler, then try to extract that functionality into another class that can be more easily unit tested. 如果要在按钮单击处理程序中测试某些内容,请尝试将该功能提取到另一个可以更轻松地进行单元测试的类中。 Keep code-behind files as thin as possible--delegating work to other classes that you can test. 使代码隐藏文件尽可能地薄-将工作委托给您可以测试的其他类。

As to points 3 and 4: 关于第3点和第4点:

As commenters have pointed out, public methods provide a rich target area for creating unit tests. 正如评论者所指出的那样,公共方法为创建单元测试提供了丰富的目标领域。 You want to ensure that the public API of your class performs as you expect, and testing the public methods gives you the opportunity to see how easy it is to use the class being tested. 您想确保您的类的公共API能够按预期执行,并且测试公共方法使您有机会了解使用被测试的类有多么容易。 If something is awkward to use in a test, it may be pointing to a need for a design change. 如果某项测试难以使用,则可能表明需要更改设计。

Only test your private methods through your public methods. 仅通过公共方法测试您的私有方法。 Unit tests ought to test behavior of your class, not the implementation. 单元测试应该测试类的行为,而不是实现。 Theoretically, you ought to be able to exercise your private methods through your public methods. 从理论上讲,您应该能够通过公共方法行使私人方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM