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:

 REM Execute the SBT tests.
 echo Execute the SBT tests.
 echo OK.

 REM Execute the SBT.
 echo Execute the SBT.
 echo OK.
 REM Exit OK.
 exit /b 0

 echo Error while executing the activator.
 exit /b 2

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.

The command mvn install is just an alias to the batch file execution.

See how to create pom.xml in the post creation of a maven project....

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.


essay writing service australia said...

I don’t do coding that much, but some of my friends who take computer classes might find this quite helpful. Will share this with them.

Amy Wong said...

I decided to start my own business, but for the start I needed some capital that I didn’t have and I decided to risk what it was and play in the casino since it wasn’t in the city online. Of all the sites, only this one with a lot of positive commatories. smart casino slot games The first two weeks I had no luck, I tried everything and the slots and gaming atoms, but after that I was in pleasant surprise and you know that now I’m doing my most favorite thing

Plastic mold said...

It was such a good post. Visit Injection molding China. Thanks for sharing.

About the author

My Photo
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.
Back to Top