本文介绍了为泛型类写单元测试...最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为另一个同事编写的代码编写单元测试。他创建了一个通用列表类。现在我正在思考,为班级创建单元测试以及如何避免重复代码的最佳方式。 List类可以通过String,Integer和其他一些类型实例化。我该如何避免为每个类型编写一个测试类,而是为每个数据类型使用一个测试类?



感谢Patrick

解决方案

您正在测试的类是否根据它们的类型以任何特殊方式处理其包含的元素?如果它将String和Integer的实例视为相同,那么您可以使用String对其进行一次测试,并假设Integer可以正常工作。



单元测试在很大程度上取决于预期的行为。

I'm supposed to write unit tests for a project whose code another colleague has written. He created a generic list class. Now i'm thinking about, the best way to create the unittests for the class and how to avoid much repetitive code. The List class can be instaniated with String, Integer and some other Types. How can i avoid to write a testclass for each of these types and instead use one testclass for every datatype?

Thanks Patrick

解决方案

Does the class you're testing treat its contained elements in any special way based on their type? If it treats instances of String and Integer the same, then you can simply test it once with String and assume that Integer will work just fine.

Unit testing depends heavily on the expected behavior.

这篇关于为泛型类写单元测试...最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:11