C# 中的单元测试链式私有方法

发布于 2024-11-14 01:58:13 字数 275 浏览 2 评论 0原文

我的问题是我的类中有公共方法,并且它正在调用私有方法。私有方法正在调用另一个私有方法,依此类推,有 4 个链式私有方法。我知道我应该只为公共方法编写单元测试。就我而言,我将具有完整的代码覆盖率,因为所有私有方法都是从公共方法调用的。但万一出现问题,我的单元测试将无法确切知道是哪种方法搞砸了。我知道我应该尝试将一些方法移到单独的类中,以便我可以测试它们,但这意味着我应该创建 4 个不同的类,每个类中只有一个方法。

那么有没有办法测试这些私有方法,或者我应该使用 Visual Studio 中的集成功能来测试私有方法?

My problem is that I have public method in my class and it is calling a private method. The private method is calling another private method and so on with 4 chained private methods. I know that I should write unit tests only for public methods. In my case I will have full code coverage, because all private methods are called from the public method. But in case something goes wrong my unit test won't know exactly which method screwed it up. I know that I should try to move some of my methods in separate classes, so that I can test them but this means that I should make 4 different classes with only one method in each.

So is there a way to test each of these private methods, or I just should use the integrated feature in Visual Studio for testing private methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-11-21 01:58:13

已经通过调用公共方法来测试私有方法。您测试公共方法的输入和输出 - 该方法依赖于私有方法,因此所有内容都在测试中。发生的任何问题都将导致异常,该异常(在检查堆栈跟踪后)将通知您哪个方法(私有或其他)导致了问题。

私人成员就是这样,私人。它们不应该暴露在外部,因为它们只关心对象的内部工作,因此单元测试应该(并且只能)测试公共测试,这就是您已经在做的事情。

You are already testing the private methods by calling the public one. You test the input and output of the public method - which relies on the private ones, hence all are being tested. Any problems that occur will result in an exception which (upon checking the stack trace) will inform you which method (private or otherwise) is causing the problem.

Private members are just that, private. They shouldn't be exposed to the outside since they're only concerned with the inner workings of the object, hence unit tests should (and can only) test the public ones, which is what you're already doing.

半边脸i 2024-11-21 01:58:13

您应该专注于测试可观察的行为,这意味着测试调用公共方法的预期结果。如果私有方法正在执行大量工作,那么请考虑将它们移动到注入到被测试对象中的对象中,如果没有,那么如果出现问题,调试起来应该不会太难:)

You should concentrate on testing observable behaviour, which means testing expected consequences of calling public methods. If the private methods are performing a lot of work then consider moving them into objects injected into the object under test, if not then if something goes wrong it shouldn't be too hard to debug :)

白况 2024-11-21 01:58:13

斯托伊曼,

我同意你的观点。

我读到很多论坛都说 PRIVET 方法不应该被测试,因为它在其他公共测试中。
但主要原因是隔离并使构建块和创建隔离级别的测试变得简单。
不是为了你,而是为了其他不同意我观点的人,这里有一个简单的例子:
公共方法 BuildCar (sizeOfChaassi, typeOfBody, Color) {
调用私有 MakeChassi (sizeOfChaassi)
调用私有 MakeBody (typeOfBody)
调用私人油漆(颜色)
}
在这种情况下,我不想公开 MakeChassi、MakeBody 和 Paint,因为我只是制造和销售汽车,但我想彻底测试我的工厂。
如果我只使用公共方法,我将需要创建大量测试变体,当然我有忘记一些变体的风险。
只是测试小部件(单元测​​试的主要原因),我很确定它们正在工作。
其他事情是在类内部,我可以创建另一个公共方法来使用这些私有方法,而不需要重新编写测试变体。

Stoimen,

I agree with you.

I read many of forums saying that PRIVET methods should not being tested because it is within other public tests.
But the main reason is to isolate and make it simple to build blocks and create isolated levels of test.
Not for you but for others that do not agree with my opinion, here is one simple example:
Public method BuildCar (sizeOfChassi, typeOfBody, Color) {
Call private MakeChassi (sizeOfChassi)
Call private MakeBody (typeOfBody)
Call private Paint (Color)
}
In this scenario, I don’t want to be public MakeChassi, MakeBody and Paint because I just build and sell cars, but I want to thoroughly test my factory.
If I just use the public method, I’ll need to create a lot of testing variations and of course I have a risk to forget some.
Just testing the small parts (the main reason of UNIT TESTs), I’m pretty sure that they are working.
Other thing is inside the class I could create another public method to use those privates, without need to re-write tests variants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文