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:
 @ECHO ON
 SET JAVA_HOME=%JDK_8%
 SET SBT_COMMAND_TEST=test
 SET SBT_COMMAND=dist

 REM Execute the SBT tests.
 echo Execute the SBT tests.
 echo.
 CALL sbt.bat %SBT_COMMAND_TEST%
 if %ERRORLEVEL% NEQ 0 GOTO BAD_EXECUTION
 echo OK.
 echo.

 REM Execute the SBT.
 echo Execute the SBT.
 echo.
 CALL sbt.bat %SBT_COMMAND%
 if %ERRORLEVEL% NEQ 0 GOTO BAD_EXECUTION
 echo OK.
 echo.
 REM Exit OK.
 exit /b 0

 :BAD_EXECUTION
 echo.
 echo Error while executing the activator.
 echo.
 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.

1 comment :

best resume service 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.

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