Я сделал пост об этом некоторое время назад.
Это возможно при использовании JUnit 4 и Ant.Хитрость заключается в том, чтобы передать имя метода [s] как свойство в командной строке и использовать другую задачу, если это свойство было передано.
Для данного класса теста:
import org.junit.Test;
public class FooTests {
@Test
public void firstTest() {
System.out.println("test 1");
}
@Test
public void secondTest() {
System.out.println("test 2");
}
@Test
public void thirdTest() {
System.out.println("test 3");
}
}
Создать этофайл ant:
<project default="test">
<!-- Tell Ant where to find JUnit -->
<path id="classpath.test">
<pathelement location="junit-4.11.jar" />
<pathelement location="hamcrest-core-1.3.jar" />
<pathelement location="." /> <!-- or where ever your test class file is -->
</path>
<target name="test" description="Runs the tests">
<!-- Set a new Ant property "use-methods" to true if the "test-methods" Ant property - which we'll pass in from the command line - exists and is not blank.-->
<condition property="use-methods" else="false">
<and>
<isset property="test-methods"/>
<not>
<equals arg1="${test-methods}" arg2=""/>
</not>
</and>
</condition>
<!-- Sanity check -->
<echo message="use-methods = ${use-methods}"/>
<echo message="test-methods = ${test-methods}"/>
<!-- Run the tests -->
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<!-- The "if" tells JUnit to only run the test task if the use-methods property is true.-->
<test if="${use-methods}" name="FooTests" methods="${test-methods}"/>
<!-- The "unless" tells JUnit to not run the test task if the use-methods property is true.-->
<test unless="${use-methods}" name="FooTests"/>
</junit>
</target>
</project>
Примеры
Запустить все тесты.
$ ant
Buildfile: build.xml
test:
[echo] use-methods = false
[echo] test-methods = ${test-methods}
[junit] Testsuite: FooTests
[junit] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] test 3
[junit] test 1
[junit] test 2
[junit] ------------- ---------------- ---------------
BUILD SUCCESSFUL
Total time: 0 seconds
Запустить только второй тест, указав «методы проверки»«Свойство Ant в командной строке.
$ ant -Dtest-methods=secondTest
Buildfile: build.xml
test:
[echo] use-methods = true
[echo] test-methods = secondTest
[junit] Testsuite: FooTests
[junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] test 2
[junit] ------------- ---------------- ---------------
BUILD SUCCESSFUL
Total time: 0 seconds
Запускайте только второй и третий тесты.
$ ant -Dtest-methods=secondTest,thirdTest
Buildfile: build.xml
test:
[echo] use-methods = true
[echo] test-methods = secondTest,thirdTest
[junit] Testsuite: FooTests
[junit] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] test 3
[junit] test 2
[junit] ------------- ---------------- ---------------
BUILD SUCCESSFUL
Total time: 0 seconds