制作一个私有方法公开进行单元测试吧...好主意吗? [英] Making a private method public to unit test it...good idea?

查看:128
本文介绍了制作一个私有方法公开进行单元测试吧...好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<分> 主持人注意: 已经有39答案贴在这里(有的已被删除)的在发布的的答案,考虑一下你是否可以添加一些有意义的讨论。你更可能只是重复什么别人已经说。


我偶尔发现自己需要做一个公共类的私有方法只是写一些单元测试它。

通常这将是因为该方法包含在类的其它方法之间共享的逻辑和它的整洁,以测试其自身的逻辑,或其他原因可能是可能的是我想测试在同步线程使用的逻辑,而不必担心关于线程问题。

不要其他人发现自己这样做,因为我真的不喜欢这样做?我个人认为奖金超过制作方法公开的问题,它并没有真正提供类以外的任何服务...

更新

感谢大家的答案,似乎激起了人们的兴趣。我认为,普遍的共识是测试通过公共API应该发生,因为这是一类将被使用的唯一途径,我也同意这种说法。这对夫妻的情况下我在上面,我会做这个上面的地方并不常见案例中提到,我认为这样做是值得的的好处。

我可以然而,每个人看到一点,它应该从来没有真正发生。并思考它的时候多一点,我认为改变你的code,以适应测试是一个坏主意 - 毕竟我想测试是在某种程度上支持工具和不断变化的系统,如果你愿意为支持支持工具 ,是明目张胆的坏习惯。


解决方案

  

注意:

  这个答案最初发布的问题是单元测试有史以来独自一个很好的理由通过干将暴露私​​有实例变量? 其并入这一项,所以它可能是具体到psented那里的用例$ P $一点点。


作为一般的说法,我通常都是对重构生产code,使其更容易测试我。不过,我不认为这是一个很好的来电来访。一个好的单元测试(通常情况下)不应该关心类的实现细节,只有它的可见行为。相反,露出内部书库的测试,可以测试类调用后返回你期望的顺序页面第()最后一个()

例如,考虑这个伪code:

 公共类NavigationTest {
    私人导航导航;    @之前
    公共无效设置(){
        //设置导航这样的顺序是page1-&GT; page2-&GT;第3页和
        //我们已经搬回到第2页
        NAV = ...;
    }    @测试
    公共无效testFirst(){
        nav.first();        的assertEquals(第1页,nav.getPage());        nav.next();
        的assertEquals(第2页,nav.getPage());        nav.next();
        的assertEquals(第3页,nav.getPage());
    }    @测试
    公共无效testLast(){
        nav.last();        的assertEquals(第3页,nav.getPage());        。资产净值previous();
        的assertEquals(第2页,nav.getPage());        。资产净值previous();
        的assertEquals(第1页,nav.getPage());
    }
}

Moderator Note: There are already 39 answers posted here (some have been deleted). Before you post your answer, consider whether or not you can add something meaningful to the discussion. You're more than likely just repeating what someone else has already said.


I occasionally find myself needing to make a private method in a class public just to write some unit tests for it.

Usually this would be because the method contains logic shared between other methods in the class and it's tidier to test the logic on its own, or another reason could be possible be I want to test logic used in synchronous threads without having to worry about threading problems.

Do other people find themselves doing this, because I don't really like doing it?? I personally think the bonuses outweigh the problems of making a method public which doesn't really provide any service outside of the class...

UPDATE

Thanks for answers everyone, seems to have piqued peoples' interest. I think the general consensus is testing should happen via the public API as this is the only way a class will ever be used, and I do agree with this. The couple of cases I mentioned above where I would do this above were uncommon cases and I thought the benefits of doing it was worth it.

I can however, see everyones point that it should never really happen. And when thinking about it a bit more I think changing your code to accommodate tests is a bad idea - after all I suppose testing is a support tool in a way and changing a system to 'support a support tool' if you will, is blatant bad practice.

解决方案

Note:
This answer was originally posted for the question Is unit testing alone ever a good reason to expose private instance variables via getters? which was merged into this one, so it may be a tad specific to the usecase presented there.

As a general statement, I'm usually all for refactoring "production" code to make it easier to test. However, I don't think that would be a good call here. A good unit test (usually) shouldn't care about the class' implementation details, only about its visible behavior. Instead of exposing the internal stacks to the test, you could test that the class returns the pages in the order you expect it to after calling first() or last().

For example, consider this pseudo-code:

public class NavigationTest {
    private Navigation nav;

    @Before
    public void setUp() {
        // Set up nav so the order is page1->page2->page3 and
        // we've moved back to page2
        nav = ...;
    }

    @Test
    public void testFirst() {
        nav.first();

        assertEquals("page1", nav.getPage());

        nav.next();
        assertEquals("page2", nav.getPage());

        nav.next();
        assertEquals("page3", nav.getPage());
    }

    @Test
    public void testLast() {
        nav.last();

        assertEquals("page3", nav.getPage());

        nav.previous();
        assertEquals("page2", nav.getPage());

        nav.previous();
        assertEquals("page1", nav.getPage());
    }
}

这篇关于制作一个私有方法公开进行单元测试吧...好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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