Data-driven tests with jUnit

2019-01-13 13:34发布

What do you use for writing data-driven tests in 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.

10条回答
Emotional °昔
2楼-- · 2019-01-13 13:39

I use an in-memory database such as hsqldb so 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 JUnit and Mockito.

查看更多
\"骚年 ilove
3楼-- · 2019-01-13 13:39

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.

查看更多
干净又极端
4楼-- · 2019-01-13 13:40

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

Unlike parameterized tests, it allows for nicely named test cases.

查看更多
【Aperson】
5楼-- · 2019-01-13 13:41

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.

查看更多
beautiful°
6楼-- · 2019-01-13 13:48

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.

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.

查看更多
闹够了就滚
7楼-- · 2019-01-13 13:50

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.

There are more drawbacks as identified in this blog post series: 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

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.

查看更多
登录 后发表回答