Java 使用 jUnit 进行数据驱动的测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1822315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data-driven tests with jUnit
提问by ripper234
What do you use for writing data-driven tests in jUnit?
你用什么来在 jUnit 中编写数据驱动的测试?
(My definition of) a data-driven test is a test that reads data from some external source (file, database, ...), executes one test per line/file/whatever, and displays the results in a test runner as if you had separate tests - the result of each run is displayed separately, not in one huge aggregate.
(我的定义)数据驱动测试是一种从外部源(文件、数据库等)读取数据,每行/文件/任何内容执行一个测试,并在测试运行器中显示结果的测试,就好像你有单独的测试 - 每次运行的结果单独显示,而不是一个巨大的聚合。
采纳答案by matt
In JUnit4 you can use the Parameterizedtestrunner to do data driven tests.
在 JUnit4 中,您可以使用参数化测试运行器进行数据驱动测试。
It's not terribly well documented, but the basic idea is to create a static method (annotated with @Parameters
) that returns a Collection of Object arrays. Each of these arrays are used as the arguments for the test class constructor, and then the usual test methods can be run using fields set in the constructor.
它没有很好的文档记录,但基本思想是创建一个@Parameters
返回对象数组集合的静态方法(用 注释)。这些数组中的每一个都用作测试类构造函数的参数,然后可以使用构造函数中设置的字段运行通常的测试方法。
You can write code to read and parse an external text file in the @Parameters
method (or get data from another external source), and then you'd be able to add new tests by editing this file without recompiling the tests.
您可以编写代码来读取和解析方法中的外部文本文件@Parameters
(或从另一个外部源获取数据),然后您可以通过编辑此文件来添加新测试,而无需重新编译测试。
回答by monksy
Typically data driven tests use a small testable component to handle the data. (File reading object, or mock objects) For databases, and resources outside of the application mocks are used to similate other systems. (Web services, and databases etc). Typically I see is that there are external data files that handle the data and the output. This way the data file can be added to the VCS.
通常,数据驱动测试使用一个小的可测试组件来处理数据。(文件读取对象,或模拟对象)对于数据库,应用程序之外的资源模拟用于模拟其他系统。(Web 服务和数据库等)。通常我看到的是有处理数据和输出的外部数据文件。这样就可以将数据文件添加到 VCS。
回答by Bostone
回答by Ed Griebel
We currently have a props file with our ID numbers in it. This is horribly brittle, but is easy to get something going. Our plan is to initially have these ID numbers overridable by -D properties in our ant builds.
我们目前有一个包含我们 ID 号的 props 文件。这是非常脆弱的,但很容易让事情发生。我们的计划是最初在我们的 ant 构建中使用 -D 属性覆盖这些 ID 号。
Our environment uses a legacy DB with horribly intertwined data that is not loadable before a run (e.g. by dbUnit). Eventually we would like to get to where a unit test would query the DB to find an ID with the property under test, then use that ID in the unit test. It would be slow and is more properly called integration testing, not "unit testing", but we would be testing against real data to avoid the situation where our app runs perfectly against test data but fails with real data.
我们的环境使用了一个遗留数据库,其中包含在运行前不可加载的可怕的交织数据(例如通过 dbUnit)。最终,我们希望到达单元测试将查询 DB 以查找具有被测属性的 ID 的位置,然后在单元测试中使用该 ID。它会很慢,更恰当地称为集成测试,而不是“单元测试”,但我们将针对真实数据进行测试,以避免出现我们的应用程序针对测试数据完美运行但在真实数据上失败的情况。
回答by digiarnie
I use an in-memory database such as hsqldbso that I can either pre-populate the database with a "production-style" set of data or I can start with an empty hsqldb database and populate it with rows that I need to perform my testing. On top of that I will write my tests using JUnitand Mockito.
我使用内存数据库,例如hsqldb,这样我就可以用“生产风格”的数据集预先填充数据库,或者我可以从一个空的 hsqldb 数据库开始,并用我需要执行的行填充它测试。最重要的是,我将使用JUnit和Mockito编写我的测试。
回答by Fortyrunner
Some tests will lend themselves to being interface driven.
一些测试将有助于接口驱动。
If the database/file reads are retrieved by an interface call then simply get your unit test to implement the interface and the unit test class can return whatever data you want.
如果数据库/文件读取是通过接口调用检索的,那么只需让您的单元测试实现接口,单元测试类就可以返回您想要的任何数据。
回答by Yishai
I'm with @DroidIn.net, that is exactly what I am doing, however to answer your question literally "and displays the results in a test runner as if you had separate tests," you have to look at the JUnit4 Parameterized runner. DBUnit doesn't do that. If you have to do a lot of this, honestly TestNG is more flexible, but you can absolutely get it done in JUnit.
我和@DroidIn.net 在一起,这正是我正在做的事情,但是要从字面上回答您的问题“并在测试运行程序中显示结果,就像您进行了单独的测试一样”,您必须查看 JUnit4 参数化运行程序。DBUnit 不会这样做。如果你必须做很多这样的事情,老实说 TestNG 更灵活,但你绝对可以在 JUnit 中完成。
You can also look at the JUnit Theories runner, but my recollection is that it isn't great for data driven datasets, which kind of makes sense because JUnit isn't about working with large amounts of external data.
您还可以查看 JUnit Theories 运行程序,但我记得它不适用于数据驱动的数据集,这是有道理的,因为 JUnit 与处理大量外部数据无关。
回答by duffymo
This is where TestNG, with its @DataSource, shines. That's one reason why I prefer it to JUnit; the others are dependencies and parallel threaded tests.
这就是 TestNG 及其@DataSource 的亮点。这就是我更喜欢 JUnit 的原因之一;其他的是依赖项和并行线程测试。
回答by Anuj
Even though this is quite an old topic, i still thought of contributing my share. I feel JUnit's support for data driven testing is to less and too unfriendly. for eg. in order to use parameterized, we need to write our constructor. With Theories runner we do not have control over the set of test data that is passed to the test method.
尽管这是一个相当古老的话题,但我仍然想贡献我的一份力量。我觉得 JUnit 对数据驱动测试的支持太少太不友好了。例如。为了使用参数化,我们需要编写我们的构造函数。使用 Theories runner,我们无法控制传递给测试方法的测试数据集。
There are more drawbacks as identified in this blog post series: http://www.kumaranuj.com/2012/08/junits-parameterized-runner-and-data.html
在此博客文章系列中发现了更多缺点:http: //www.kumaranuj.com/2012/08/junits-parameterized-runner-and-data.html
There is now a comprehensive solution coming along pretty nicely in the form of EasyTest which is a a framework extended out of JUnit and is meant to give a lot of functionality to its users. Its primary focus is to perform Data Driven Testing using JUnit, although you are not required to actually depend on JUnit anymore. Here is the github project for refernece: https://github.com/anujgandharv/easytest
现在有一个以 EasyTest 形式出现的综合解决方案非常好,它是一个从 JUnit 扩展的框架,旨在为其用户提供许多功能。它的主要重点是使用 JUnit 执行数据驱动测试,尽管您实际上不再需要依赖 JUnit。这是参考的github项目:https: //github.com/anujgandharv/easytest
If anyone is interested in contributing their thoughts/code/suggestions then this is the time. You can simply go to the github repository and create issues.
如果有人有兴趣贡献他们的想法/代码/建议,那么现在是时候了。您可以简单地转到 github 存储库并创建问题。
回答by Mathieu
You are better off extending TestCase with a "DataDrivenTestCase" that suits your needs. Here is working example: http://mrlalonde.blogspot.ca/2012/08/data-driven-tests-with-junit.html
您最好使用适合您需求的“DataDrivenTestCase”扩展 TestCase。这是工作示例:http: //mrlalonde.blogspot.ca/2012/08/data-driven-tests-with-junit.html
Unlike parameterized tests, it allows for nicely named test cases.
与参数化测试不同,它允许很好地命名测试用例。