Automated testing is seen as a key part of Grails, implemented using
Groovy Tests. Hence, Grails provides many ways to making testing easier from low level unit testing to high level functional tests. This section details the different capabilities that Grails offers in terms of testing.
The first thing to be aware of is that all of the
create-*
commands actually end up creating
integration
tests automatically for you. For example say you run the
create-controller command as follows:
grails create-controller simple
Not only will Grails create a controller at
grails-app/controllers/SimpleController.groovy
, but also an integration test at
test/integration/SimpleControllerTests.groovy
. What Grails won't do however is populate the logic inside the test! That is left up to you.
As of Grails 1.2.2,the suffix of Test
is also supported for test cases.
Running Tests
Test are run with the
test-app command:
The above command will produce output such as:
-------------------------------------------------------
Running Unit Tests…
Running test FooTests...FAILURE
Unit Tests Completed in 464ms …
-------------------------------------------------------Tests failed: 0 errors, 1 failures
Whilst reports will have been written out the
target/test-reports
directory.
You can force a clean before running tests by passing -clean
to the test-app
command.
Targeting Tests
You can selectively target the test(s) to be run in different ways. To run all tests for a controller named
SimpleController
you would run:
grails test-app SimpleController
This will run any tests for the class named
SimpleController
. Wildcards can be used...
grails test-app *Controller
This will test all classes ending in
Controller
. Package names can optionally be specified...
grails test-app some.org.*Controller
or to run all tests in a package...
grails test-app some.org.*
or to run all tests in a package including subpackages...
grails test-app some.org.**
You can also target particular test methods...
grails test-app SimpleController.testLogin
This will run the
testLogin
test in the
SimpleController
tests. You can specify as many patterns in combination as you like...
grails test-app some.org.* SimpleController.testLogin BookController
Targeting Test Types and/or Phases
In addition to targeting certain tests, you can also target test
types and/or
phases by using the
phase:type
syntax.
Grails organises tests by phase and by type. A test phase relates to the state of the Grails application during the tests, and the type relates to the testing mechanism.Grails comes with support for 4 test phases (unit
, integration
, functional
and other
) and JUnit test types for the unit
and integration
phases. These test types have the same name as the phase.Testing plugins may provide new test phases or new test types for existing phases. Refer to the plugin documentation.
To execute the JUnit
integration
tests you can run:
grails test-app integration:integration
Both
phase
and
type
are optional. Their absence acts as a wildcard. The following command will run all test types in the
unit
phase:
The Grails
Spock Plugin is one plugin that adds new test types to Grails. It adds a
spock
test type to the
unit
,
integration
and
functional
phases. To run all spock tests in all phases you would run the following:
To run the all of the spock tests in the
functional
phase you would run...
grails test-app functional:spock
More than one pattern can be specified...
grails test-app unit:spock integration:spock
Targeting Tests in Types and/or Phases
Test and type/phase targetting can be applied at the same time:
grails test-app integration: unit: some.org.**
This would run all tests in the
integration
and
unit
phases that are in the page
some.org
or a subpackage of.