具有一个公共和多种私有方法的单元测试类 [英] Unit test class with one public and multiple private methods

查看:242
本文介绍了具有一个公共和多种私有方法的单元测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何进行以下测试以便对类进行单元测试时遇到了一些麻烦.

I'm having a little trouble understanding how to approach the following in order to unit test the class.

被测试的对象是一个由1个公共方法组成的对象,该方法接受一个类型为A的对象列表并返回一个对象B(它是二进制流). 由于生成的二进制流的性质,该二进制流会变得很大,因此对于测试输出而言,这不是一个很好的比较. 该流是使用几种私有实例帮助器方法构建的.

The object under test is an object that consists out of 1 public method, one that accepts a list of objects of type A and returns an object B (which is a binary stream). Due to the nature of the resulting binary stream, which gets large, it is not a nice comparison for the test output. The stream is built using several private instance helper methods.

class Foo
{
    private BinaryStream mBinaryStream;
    public Foo() {}
    public BinaryStream Bar(List<Object> objects) {
        // perform magic to build and return the binary stream;
        // using several private instance helper methods.
        Magic(objects);
        MoreMagic(objects);
    }
    private void Magic(List<Object> objects) { /* work on mBinaryStream */ }
    private void MoreMagic(List<Object> objects) { /* work on mBinaryStream */ }
};

现在我知道我需要测试类的行为,因此需要测试Bar方法. 但是,将方法的输出与预定义的结果进行比较是不可撤销的(无论是在时间上还是在时间上). 变体的数量太大(它们是极端的情况).

Now I know that I need to test the behaviour of the class, thus the Bar method. However, it's undoable (both space and time wise) to do compare the output of the method with a predefined result. The number of variations is just too large (and they are corner cases).

一个可行的选择是将这些私有帮助器方法重构为可以进行单元测试的一个或多个单独的类.然后可以将二进制流切成较小的更好的可测试块,但是在此还需要处理很多情况,并且比较二进制结果将无济于事单元测试的快速时间.这是我不想去的选择.

One option to go for is to refactor these private helper methods into (a) separate class(es) that can be unit tested. The binary stream can then be chopped into smaller better testable chunks, but also here goes that a lot of cases need to be handled and comparing the binary result will defy the quick time of a unit test. It an option I'd rather not go for.

另一个选择是创建一个定义所有这些私有方法的接口,以验证(使用模拟)是否调用了这些方法.但是,这意味着这些方法必须具有公众可见性,这也不是一件好事.验证方法调用可能足以进行测试.

Another option is to create an interface that defines all these private methods in order to verify (using mocking) if these methods were called or not. This means however that these methods must have public visibility, which is also not nice. And verifying method invocations might be just enough to test for.

另一种选择是从类继承(使私有对象受保护)并尝试以这种方式进行测试.

Yet another option is to inherit from the class (making the privates protected) and try to test this way.

我已经阅读了有关此类问题的大多数主题,但是它们似乎可以处理良好的可测试结果.这与挑战不同.

I have read most of the topics around such issue, but they seem to handle good testable results. This is different than from this challenge.

您将如何对此类课程进行单元测试?

How would you unit test such class?

推荐答案

那么,您首先要了解如何处理私有方法.测试流以获取正确的输出.就我个人而言,我将使用一组非常有限的输入数据,并且只需在单元测试中执行代码即可.

Well you are on top of how to deal with the private methods. Testing the stream for the correct output. Personally I'd use a very limited set of input data, and simply exercise the code in the unit test.

我将所有潜在情况视为集成测试.

All the potential scenarios I'd treat as an integration test.

因此有一个包含输入和预期输出的文件(例如xml).运行它,用输入调用该方法,并将实际输出与预期的报告差异进行比较.因此,您可以在签到时进行此操作,也可以在将其部署到UAT或诸如此类之前进行此操作.

So have a file (say xml) with input and expected output. Run through it, call the method with the input and compare actual output with expected, report differences. So you could do this as part of checkin, or before deploy to UAT or some such.

这篇关于具有一个公共和多种私有方法的单元测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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