Monday, June 12, 2017

Play unit tests execution

Anyone coming from the Java/maven ecosystem is used to run unit tests in all combinations: all tests upon building the project, from IDE (i.e. eclipse) as an individual test class or as a particular test case inside the class.
Let's see how this may be done for a Play project.

Running tests with sbt

The command sbt test compiles the Play project and runs all the tests, defined under the project folder "test" - no matter if it is Java unit tests, Scala units tests or whatever.

The sbt dist compiles the project and makes packaging, but it does not call tests execution implicitly.

In order to run tests before packaging the following sequence should be done:

sbt test
sbt dist

Note, that the package will be created in any case, even if some of the tests failed.

Project packaging should be dependent on successful tests execution.
The following is an example of batch files for windows, which prevents project packaging in case of tests failure:

The effect of running such batch file is similar to running the mvn install: the target will be created only if all tests are OK.

Running tests with maven

If a Play project has in the root directory pom.xml, it may be managed with maven. All what is needed in the pom.xml is an execution plugin, which runs the batch file similar to shown in the above example.

Running unit tests from eclipse

Note: It took significant efforts to figure out the solution. Since I am working with a maven project, I cannot say, if the procedure below works for not-maven project.

To run unit tests from eclipse the following steps should be done:
1. Create a pom.xml for the Play project like it is explained in the post creation of a maven project....
2. Create an eclipse project with the command sbt eclipse like it is explained in the post eclipse IDE setting....
3. Import the project into eclipse via File->Import->Existing Maven Project.
4. Configure the class path: right-click on the project and select Scala->Add Scala Library to Build Path.

About the author

I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.