简体   繁体   English

关于私人方法的单元测试的问题

[英]Questions on unit testing of private methods

In MSTest, the [Shadowing] attribute helps you to unit test a private method from another assembly. 在MSTest中, [Shadowing]属性可帮助您从另一个程序集中对单个方法进行单元测试。 Here is the related link : What is the Shadowing attribute that the VS uses when it generates unit tests? 以下是相关链接: VS在生成单元测试时使用的Shadowing属性是什么?

My questions are: 我的问题是:

  1. Should private methods be unit tested separately? 私人方法应该单独进行单元测试吗?
  2. Is it a good (?) practice to change private method's accessor to internal just to make it available for unit testing in some other test project/assembly? 将私有方法的访问者更改为internal只是为了使其可用于某些其他测试项目/程序集中的单元测试,这是一种很好的(?)实践吗? (using InternalsVisibleTo ) (使用InternalsVisibleTo
  3. If private methods get tested indirectly through the public method that calls them, can it be called "unit" testing? 如果通过调用它们的公共方法间接测试私有方法,它可以称为“单元”测试吗?
  1. No, private methods should not be tested. 不,不应该测试私有方法。 Your application will interact with public API only. 您的应用程序仅与公共API交互。 So you should test expected behavior of your class for this interaction. 因此,您应该测试您的类的预期行为以进行此交互。 Private methods is a part of inner logic implementation. 私有方法是内部逻辑实现的一部分。 Users of your class should not be care how it implemented. 你班上的用户不应该关心它是如何实现的。
  2. No, it is not good. 不,这不好。 See above. 往上看。 You should test public API only. 您应该只测试公共API。
  3. You should test only public methods. 您应该只测试公共方法。 You don't care if public method will call to private method or will not until test is passed. 您不关心公共方法是否会调用私有方法,或者在测试通过之前不会。 If test fails, fix implemetation. 如果测试失败,请修复实现。 But don't test private methods anyway. 但是不要测试私有方法。

UPDATE (how to define what to test): Ideally (in test-first approach) test is the first user of your class. 更新(如何定义要测试的内容):理想情况下(在测试优先方法中)测试是您班级的第一个用户。 When you write test, you try to imagine how users will use your class. 当您编写测试时,您会尝试想象用户将如何使用您的课程。 Users will not interact with private methods (Reflection is cheating). 用户不会与私人方法交互(反思是作弊)。 So and your test, as first user of your class, should not interact with private methods. 因此,作为您班级的第一个用户,您的测试不应与私有方法交互。

To answer your questions briefly: 简要回答一下你的问题:

  1. In general, they shouldn't. 一般来说,他们不应该。 Most of the times, your private bits will be tested while testing class contract/public API. 大多数情况下,您的私有位将在测试类合同/公共API时进行测试。 For the times this is not possible, testing private method is unit test just as anything else. 对于时间这是不可能的,测试私有方法就像其他任何东西一样是单元测试。

  2. This is fairly common. 这很常见。 While changing visibility might be considered bad idea, it's not as bad when it changes only to internal. 虽然改变可见性可能被认为是坏主意,但当它仅仅改变为内部时,它并没有那么糟糕。 However, in approaches like TDD, the need of testing usually drives your desing in such way that "hacks" like this are not needed. 然而,在像TDD这样的方法中,测试的需要通常会以这样的方式驱动你的设计,以至于不需要这样的“黑客”。 But like I said, it's fairly common - you shouldn't worry too much about it, unless it gets to ridiculous levels (say, entire private parts of classes exposed). 但就像我说的那样,这是相当普遍的 - 你不应该过多担心它,除非它达到荒谬的程度(比如暴露的整个私人部分)。

  3. It is unit test as long as it tests single unit (or, one logical concept ) of your class. 它是单元测试,只要它测试你的类的单个单元 (或一个逻辑概念 )。 Private methods more often than not are created as a results of refactorings in public parts, which most of the times will be targeted by the single unit testing. 私有方法通常是作为公共部分重构的结果而创建的,大部分时间都是单个单元测试的目标。 If you feel your private method is not longer an unit , it might be a refactoring call. 如果您觉得您的私有方法不再是一个单元 ,那么它可能是一个重构调用。

Also, I suggest having a look here , here and here . 另外,我建议看看这里这里这里

I would preffer to exercise all the private methods thru the public available calls. 我希望通过公共可用的电话来运用所有私人方法。 There must be a path to execute each private line from a public call and if it is not there you can delete that code. 必须有一条路径来执行公共呼叫中的每条专线,如果不存在,您可以删除该代码。

Using internal instead of private could end with a big mess, I won't use that approach. 使用内部而不是私有可能会以一个大混乱结束,我不会使用这种方法。

As a Java developer, I do. 作为Java开发人员,我这样做。 I don't change access levels, either. 我也没有改变访问级别。 I use reflection to get access to private methods. 我使用反射来访问私有方法。 I don't have to grant it to my users, and I don't have to show them my unit tests. 我不必将它授予我的用户,而且我不必向他们展示我的单元测试。

It's a dirty secret of Java: you can always circumvent access restrictions with reflection. 这是Java的一个肮脏秘密:您始终可以通过反射来规避访问限制。 I don't know if it's also true of C# and .NET, but you can look into it to see. 我不知道C#和.NET是否也是如此,但你可以查看它。

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

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