Testing Grails 3 app with 'gradle test' uses wrong environment config - grails

I have a Grails 3 application that I'm trying to configure an embedded datastore for functional tests for. I have the configuration for the datastore specific to the 'test' environment.
When I run 'grails test-app', the app connects to the correct datastore and my functional tests pass.
When I try testing the application with 'gradle test', it tries to connect to the datastore for the development environment and fails.
I have tried specifying the Grails environment to use for the gradle test task by adding this to build.gradle:
test {
String testEnvArg = '-Dgrails.env=test'
if (jvmArgs) {
jvmArgs.add(testEnvArg)
} else {
jvmArgs = [testEnvArg]
}
}
But the behavior appears to be unchanged.
How can I make the gradle 'test' task use the correct Grails environment configuration?

You must specify environment:
gradle -PgrailsEnv=test test

To boot run with production environment and gradle wrapper below worked:
./gradlew -Dgrails.env=production bootRun
And with requested test environment:
./gradlew -Dgrails.env=test bootRun

Related

How to run a specific test or class using jenkins pipeline

I'm using this command to run my tests:
sh "${mvnHome}/bin/mvn clean test -e -Dgroups=categories.dbd"
but sometimes I want to run a specific test. How can I do it?
I read that I can use "This project is parameterized" but didn't understand how to use it.
I also saw this - https://plugins.jenkins.io/selected-tests-executor/ but it's not good enough since it required an external file.
If you use the maven-surefire-plugin you can simply run
sh "${mvnHome}/bin/mvn clean test -e -Dgroups=categories.dbd -Dtest=com.example.MyJavaTestClass"
or
sh "${mvnHome}/bin/mvn clean test -e -Dgroups=categories.dbd -Dtest=com.example.MyJavaTestClass#myTestMethod"
I suggest to add a parameter for the test class/method to your pipeline definition.
pipeline {
agent any
parameters {
string defaultValue: '', description: 'Test Name', name: 'TEST_NAME', trim: false
}
stages {
stage('run tests') {
steps {
script {
def optionalParameters = ""
if (params.TEST_NAME != null) {
optionalParameters += " -Dtest=" + params.TEST_NAME
}
sh "${mvnHome}/bin/mvn clean test -e -Dgroups=categories.dbd" + optionalParameters
}
}
}
...
}
...
}
Jenkins isn't really the tool for that. It's typical use case us running all tests. If you really want to run only one test, you should do that in your development environment
But the simplest thing to do, if you want the results for one of the tests, us to have Jenkins run all your tests and simply ignore the results for the other tests.
Generally, you should run (quick, cheap) unit tests in your development environment, and submit code for (expensive, slow) integration tests by Jenkins only once the code passes the unit tests (And Jenkins should run the unit tests again, just in case).
I suspect your real question is "how do I debug failure of an integration test run by Jenkins". Jenkins is a build and test tool not a debugging tool. It is not itself a suitable tool for debugging test failures. But the way you use Jenkins can help debugging.
Do not use integration tests as a substitute for unit tests.
If your software fails an integration test, but no unit tests, as usual with debugging a test failure you should be making hypotheses about the kinds of defect in your software that could cause the failure. Then check whether a unit test could be added that would detect that kind of defect. If so, add that unit test.
Ensure that your tests produce useful diagnostic messages on failure. Test assertions should have helpful messages. Test a should have descriptive names.
If an integration test checks a sequence of actions, ensure you also have tests for the individual actions.

How to configure, in a Jenkinsfile, the database to be used by the test suite of a Rails app in Jenkins?

I have a Rails app which uses a PostgreSQL database, and I want to run the test suite in Jenkins. I started looking for the configuration of a Jenkinsfile to execute the test suite, and kind of understand the usage of agents to execute the Rails app inside a container. But I haven't found any reference regarding how to configure a database to be used by the tests.
Do I have to configure the database in my Jenkinsfile?
If so, how do I do it? The DB will be another agent?
If so, how to configure a second agent (all the examples I've found have only one agent)?
You can configure the database in a rails app via database.yml or ENV['DATABASE_URL'].ENV['DATABASE_URL'] is actually merged with database.yml but the env var takes precedence.
You can set ENV vars in your Jenkinsfile though environment:
pipeline {
environment {
DATABASE_URL= 'postgresql://localhost/my_test_database?pool=5'
}
# ...
}
You may want to use credentials through instead of hard-coding your db username/password into your Jenkinsfile.
pipeline {
environment {
DATABASE_URL= credentials('jenkins-database-url')
}
# ...
}
If you want to split this into multiple configuration options your can either use interpolation in your Jenkinsfile or use ERB in your database.yml

Why does my Gradle Jenkins plugin try to pass my command line switches before the tasks

I'm trying to run my tests in Jenkins with Gradle. I have a gradle task called 'test' and I specify which tests to run with --tests
Using the Jenkins Gradle plugin, I've specified the Switches (--tests ) and Tasks ('test') but when the build actually runs it puts the --tests stuff before the tasks which doesn't work at all. I need the task to come first on the commandline before the switches otherwise it doesn't work. What can I do here?
If you add --tests to Tasks in the right order it should work.
I.e. try adding
test --tests <test include expression>
as Tasks.
Switches is just for convenience - in the end everything will end up as the argument list of the gradle executable.

using multiple JUnit results file on Jenkins pipeline

On my Jenkins pipleline I run unit tests on both: debug and release configurations. Each test configuration generates separate JUnit XML results file. Test names on both: debug and release configuration are same. Currently I use the following junit command in order to show test results:
junit allowEmptyResults: true, healthScaleFactor: 0.0, keepLongStdio: true, testResults: 'Test-Dir/Artifacts/test_xml_reports_*/*.xml'
The problem is that on Jenkins UI both: debug and release tests results are shown together and it is not possible to know which test (from debug or release configuration) is failed.
Is it possible to show debug and release tests results separately? If yes, how can I do that?
We run the same integration tests against two different configurations with different DB types. We use maven and the failsafe plugin, so I take advantage of the -Dsurefire.reportNameSuffix so I can see the difference between the two runs.
The following is an example block of our Jenkinsfile:
stage('Integration test MySql') {
steps {
timeout(75) {
sh("mvn -e verify -DskipUnitTests=true -DtestConfigResource=conf/mysql-local.yaml " +
"-DintegrationForkCount=1 -DdbInitMode=migrations -Dmaven.test.failure.ignore=false " +
"-Dsurefire.reportNameSuffix=MYSQL")
}
}
post {
always {
junit '**/failsafe-reports/*MYSQL.xml'
}
}
}
In the report, the integration tests run against mysql then show up with MYSQL appended to their name.
It looks no solution for my question.
As a workaround I changed JUnit XML report format and included build variant name (debug/release) as a package name.

Is it possible to start grails server application in junit

I have a grails application and I'm writing testcases in junit. is there any way to start grails server using JUNIT.
You can run your unit test by --
Run as JUnit Test
or otherwise
test-app: Runs all Grails unit and integration tests and generates reports.
In your case you could do:
test-app unit:
or
test-app integration:
or for a specific ExampleTests.groovy file:
test-app unit: Example

Resources