Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tomek Kaczanowski - Practical Unit Testing with JUnit and Mockito - 2013.pdf
Скачиваний:
228
Добавлен:
07.03.2016
Размер:
6.59 Mб
Скачать

Appendix B. Running Unit Tests

Listing B.2. Output of "gradle test" command

>gradle test

:compileJava

:processResources

:classes

:compileTestJava

:processTestResources

:testClasses

:test

BUILD SUCCESSFUL

The last printed line assures us that all tests have passed successfully. You can make Gradle print much more information - also regarding executed tests - by using -i switch, or by writing your own listener (see Section 8.3).

In the event of failed tests, Gradle will print error messages. Please refer to Section 8.2 for more information on the reports which could be used to investigate the failed tests.

The gradle test command will execute all tests in your project - which, by the way, is advisable. To run a single test use the gradle -Dtest.single=MoneyTest test syntax.

B.3.1. Using JUnit Listeners with Gradle

Unfortunately, at the time of writing Gradle does not provide any help when it comes to custom listeners (see Section 8.3). In particular, it does not allow for specifying which listeners should be used when running all tests. Taking into consideration the rapid development of Gradle, we can expect this shortcoming to be addressed in the not-so-distant future.

B.3.2. Adding JARs to Gradle’s Tests Classpath

In the event of your tests requiring some additional libraries, you will need to add them to the testCompile scope together with the relevant JUnit dependency, for example:

Listing B.3. Gradle - adding libraries required by test classes

dependencies {

testCompile 'junit:junit:4.11' testCompile 'joda-time:joda-time:2.1'

testCompile 'com.practicalunittesting.listeners:1.0'

}

The configuration shown above would result in having JUnit, Joda Time1 and some custom listeners available on the test classpath.

B.4. Running Tests with Maven

To use Maven for running your tests you should at least read the documentation for the Maven Surefire Plugin2, and in particular its section devoted to running JUnit tests with Maven. In addition, read the

1http://joda-time.sourceforge.net/

2http://maven.apache.org/plugins/maven-surefire-plugin/

274

Appendix B. Running Unit Tests

documentation for the Maven Surefire Report Plugin3 and the Maven Site Plugin4. Both can be used to generate nice HTML test reports. If you plan to use Maven for running more than unit tests you should take a look at the Maven Failsafe Plugin5. In any case, make sure you have an understanding of basic concepts of Maven - for example, its lifecycle. If you try using Maven without such knowledge, you will very quickly become frustrated.

This section provides basic information about running tests with Maven. There is much more to know about Maven (which is quite a complex tool, with all sorts of quirks of its own), but what is presented here should be enough to enable you to execute your unit tests with Maven.

As with Gradle, Maven will be happy with the default layout of your project. You need only inform Maven that it should use the JUnit dependency in the scope test (which makes Maven add JUnit JAR to the classpath during test execution).

Listing B.4. Basic Maven build script

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd ">

<modelVersion>4.0.0</modelVersion>

<groupId>com.practicalunittesting</groupId> <artifactId>running-tests-with-maven</artifactId> <version>1.0-SNAPSHOT</version> <url>http://practicalunittesting.com</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

Now execute mvn test from the command line. Maven will automatically recognize JUnit test classes located in src/test/java and run them. You will see something similar to the following output (some lines omitted):

3http://maven.apache.org/plugins/maven-surefire-report-plugin/

4http://maven.apache.org/plugins/maven-site-plugin/

5http://maven.apache.org/plugins/maven-failsafe-plugin/

275

Appendix B. Running Unit Tests

Listing B.5. Output of "mvn test" command

>mvn clean test

[INFO] --------------------------------------------------------------

[INFO] Building running-tests-with-maven 1.0-SNAPSHOT

[INFO] --------------------------------------------------------------

---------------------------------------------

T E S T S

---------------------------------------------

Running com.practicalunittesting.appendixb.SampleTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] --------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] --------------------------------------------------------------

The last printed line assures us that all tests have passed successfully.

In the event of failed tests, Maven will print error messages to the console. Test results are kept in the target/surefire-reports directory in text, and XML format. If you prefer something more readable, Maven makes it simple to generate an HTML report as well. As was mentioned previously, you could use the Maven Surefire Report Plugin or the Maven Site Plugin to achieve this.

B.4.1. Using JUnit Listeners and Reporters with Maven

Maven lets you specify which listeners (see Section 8.3) should be used when running tests. This is done by passing the fully qualified class name as a configuration parameter of the Maven Surefire Plugin. An example is shown in Listing B.6.

Listing B.6. Using a custom JUnit listener with Maven

<plugin>

<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12</version>

<configuration>

<properties>

<property>

<name>listener</name>

<value>com.practicalunittesting.TimeTestListener</value>

</property>

</properties>

</configuration>

</plugin>

B.4.2. Adding JARs to Maven’s Tests Classpath

To add other libraries to the test classpath (so that, for example, you can use Hamcrest in your test code), you will need to specify them in the same way that we specified the JUnit dependency. For example:

276

Appendix B. Running Unit Tests

Listing B.7. Maven - adding libraries required by tests classes

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope> </dependency> <dependency>

<groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.1</version>

<scope>test</scope> </dependency> <dependency>

<groupId>com.practicalunittesting</groupId>

<artifactId>listeners</artifactId>

<version>1.0</version>

<scope>test</scope>

</dependency>

</dependencies>

Make sure you use the test scope so Maven adds them to the test classpath.

277

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]