7

How do I run a Junit 4.8.1 Test suite from command line ? Also I want to use the categories introduces with JUnit 4.8 , is there a way where I can specify from command line the category which I want to run.

topchef
  • 19,091
  • 9
  • 63
  • 102
user448070
  • 4,023
  • 3
  • 16
  • 11

4 Answers4

5

Using java run JUnitCore class (also see here).

Categories are supposed to be used with test suites with @RunWith(Categories.class) , @IncludeCategory and @ExcludeCategory. I am not aware of any dynamic way to use categories to run tests but I'd like to know of such it it exists. You can have pre-defined test suites for certain categories to run them.

topchef
  • 19,091
  • 9
  • 63
  • 102
  • It's possible to create custom test runner that dynamically selects tests to run based on categories. – topchef Oct 22 '10 at 14:52
  • Here's one way to easily run all tests in a suite/category (without explicitly enumerating the tests): http://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4/2176791#2176791 (Let me know if this is not what you meant.) – Jonik Jun 03 '11 at 09:45
  • Yes, I am aware of this (http://stackoverflow.com/questions/3324623/how-can-i-run-all-junit-tests-in-one-package-netbeans/3332970#3332970). I think what author means is running tests based on category as a parameter. – topchef Jun 03 '11 at 15:39
  • The obvious reason to do this is to exclude slow tests (i.e. touches, network, database or disk) from our 10 minute CI build. This gives organizations that have been misusing JUnit a way to filter out that misuse. – Mark Levison Dec 17 '12 at 15:27
3

There is no way (as of 4.8) to specify categories from the command line.

Kent Beck
  • 5,011
  • 2
  • 28
  • 31
1

I can suggest two approaches: 1. Create Ant file with junit target and then invoke this target from commend line. 2. Implement test suite class, in it in some class with main() method. So you will be able to run it.

Andriy Sholokh
  • 852
  • 2
  • 6
  • 15
0

In 4.10, we do this:

mvn verify -p(your profiles) -Dit.test=(SuiteClass)

where SuiteClass is an empty class (no methods or fields) that is annotated with @RunWith(Categories.class) and @Suite.SuiteClasses({FooIT.class, BarIT.class, ...}). FooIT and BarIT are the integration tests.

Mark
  • 4,970
  • 5
  • 42
  • 66