如何在测试中覆盖100%的项目? [英] How do I cover 100% of my project in tests?

查看:140
本文介绍了如何在测试中覆盖100%的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了一个初级.Net开发人员的新工作,我的第一个任务是覆盖一个在单元测试中取得80%成功的解决方案,以确保每一段代码都能正常工作。

我可以通过简单地从我的测试中调用它来覆盖所有公共方法后达到37%,但我不知道如何从类之外访问私有/受保护的方法。

问题是我不允许更改任何现有代码。



项目是用C#.net框架编写的4.7.2
对于代码覆盖率分析,我使用OpenCover

测试框架:Nunit 3.11.0



任何建议表示赞赏!



我尝试了什么:



我试图找到一个解决方案这个问题,但不幸的是,最常见的建议是不测试私有方法。

另一种选择是将方法改为公开,但它不是一个选项我不允许更改任何现有代码。

I just started a new job as a junior .Net developer and my first task is to cover a solution that has 80% success with unit-tests, in order to assure every piece of code is working correctly.
I was able to make it to 37% after covering all public methods by simply calling them from my tests, but I have no idea how to access private/protected methods from outside of their classes.
The catch is that I am not allowed to change any piece of the existing code.

The project is written in C# .net framework 4.7.2
For Code coverage analysis I use OpenCover
Testing framework: Nunit 3.11.0

Any advice is appreciated!

What I have tried:

I have tried to find a solution for this issue, but unfortunately, the most common advice is to simply not test Private methods.
Another alternative was changing the methods to public, but it's not an option as I am not allowed to change any piece of the existing code.

推荐答案

我们的团队使用Typemock的Isolator作为模拟框架,它能够测试私有方法。 br />


用于验证私有方法调用的简单Api:

Isolate.Verify.NonPublic.WasCalled(< instance>,PrivateMethodname);

此外,您可以伪造私有实例并更改其行为,这将有助于您抵御数据库调用以及所有这些紧密的依赖关系



它有一个缺点,根本不便宜,但如果我没有弄错,它有7天的免费试用。

下载隔离器 - Typemock [ ^ ]



希望有帮助
Our team uses Typemock's Isolator as a mocking framework and it has the ability to test private methods.

Simple Api for verifying private method calls:
Isolate.Verify.NonPublic.WasCalled(<instance>, "PrivateMethodname");
Also, you can fake private instances and change their behavior which will help you against the DB calls and all this tight dependencies

It has a disadvantage, not cheap at all but if i am not mistaken it has 7 days of free trial.
Download Isolator - Typemock[^]

hope it helps


1。请解释一下这意味着什么:一个有80%成功的解决方案



2.一般来说,你不会对私人方法进行单元测试;毕竟,根据定义,它们仅用于公共API



3.当然,应用程序的体系结构可能如此搞砸,以至于你无法隔离某些班级;正如您所描述的那样,您可能无法更改代码等。单元测试无法触及结构性问题。



4.如果你真的不得不弄乱使用私有方法,您可以使用反射:[ ^ ]



5.你可以使用AOP工具比如PostSharp(商业)[ ^ ]和Fody(开源)[ ^ ]将日志记录代码注入/编织到运行时应用程序中



我建议你和你的团队成员和/或经理谈谈,并对你的期望做一些澄清。没有人会为此减少对你的评价!



imho,单元测试是更广泛的软件过程的关键部分,包括记录,识别瓶颈以及错误,性能调整,以及拥有真实的人(最好是幼稚的最终用户)砰然一声,试着打破它。
1. please explain what this means: "a solution that has 80% success"

2. in general you do not unit test private methods; after all, by definition they are only used from the public API

3. of course, the architecture of an app can be so screwed up that you can't isolate certain classes; as you describe, you may not be allowed to change the code, etc. Unit testing can't touch structural issues that.

4. if you really have to mess around with private methods, you could use reflection: [^]

5. you could use AOP tools like PostSharp (commercial) [^], and Fody (open source) [^] to inject/weave logging code into the run-time app

I suggest you talk to your team members and/or manager, and get some clarification on what is expected of you. No one will think less of you for this !

imho, unit testing is a key part of a wider process of software qa that includes logging, identifying bottlenecks as well as bugs, performance tuning, and having real people (preferably "naive" end-users) bang away at your whatever and try to break it.


坏消息是你的任务可能是不可能的。单元测试不是你可以改装到应用程序的东西,它必须记住单元测试。尝试将单元测试改编为不适合它的应用程序通常会导致代码的重大重构。



为了涵盖您的具体问题,您倾向于通过操纵调用这些方法的公共方法来确保私有方法的覆盖。所以,如果我有如下代码;



The bad news is that your task is probably impossible. Unit testing isn't something you can retro-fit to an application, it has to be written with unit testing in mind. Trying to retro-fit unit testing to an app not designed for it will often lead to major refactoring of the code.

To cover your specific question though, you tend to ensure coverage in private methods by manipulating the public methods that call those methods. So if I have code like below;

public class TestClass
{
    public int Add(int x, int y)
    {
        if (!IsValid(x) || !IsValid(y))
        {
            throw new ArgumentException();
        }

        return x + y;
    }

    private bool IsValid(int number)
    {
        if (number >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}





我不能直接从我的单元测试中调用IsValid,但是我可以确保函数中的所有路径都通过使用有效和无效参数调用Add来覆盖;





I can't call IsValid directly from my unit tests, but I can make sure all paths in the function are covered by calling Add with both valid and invalid params;

[TestMethod]
public void WhenParamsValidAddReturnsSum()
{
    // Arrange
    TestClass testClass = new TestClass();

    // Act
    int result = testClass.Add(1, 2);

    // Assert
    Assert.AreEqual(3, result);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void WhenParamsInvalidAddReturnsException()
{
    // Arrange
    TestClass testClass = new TestClass();

    // Act
    int result = testClass.Add(-1, 2);
}





请仔细阅读



< a href =https://docs.microsoft.com/en-us/visualstudio/test/unit-test-your-code?view=vs-2017>单元测试 - Visual Studio | Microsoft Docs [ ^ ]


这篇关于如何在测试中覆盖100%的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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