Running tests in a spec in a sequence - geb

Is there a way to run the tests within a spec in sequence?
#Stepwise
class TestSpec extends GebReportingSpec {
def 'A'(){
//
}
def 'B'(){
//
}
def 'C'(){
//
}
}
Is there a way I can force A to run before B and B to run before C?
Thanks,
Shravanthi

It should run in this order but with #Stepwise, you will keep the session between the features.

Related

How to run multiple test suites in another test suite with geb

I would like to run all my test suites. I want to call the test suites in one test suit called Allrun.
Can I call test suites in another test suite. I am looking for something like this:
class Myfirst extends GebReportingSpec {
def myfunction(){
when:''
at mypage1
and:''
element1.text() == "mytext1"
}
}
class Mysecond extends GebReportingSpec {
def mysecondFunction() {
when:''
at mypage2
and:''
element2.text() == "mytext2"
}
}
class AlltestSuites extends GebReportingSpec {
Myfirst myfirst = new Myfirst ()
Mysecond mysecond = new Mysecond ()
def allrun(){
myfirst.myfunction()
mysecond.mysecondFunction()
}
}
How can i do this? Does anyone has an idea
You can create a AllRun.groovy file like below to run all class files in the specified order [Myfirst.class, Mysecond.class, etc].
package geb.groovy
import org.junit.runner.RunWith
import org.junit.runners.Suite
import geb.groovy.scripts.Myfirst
import geb.groovy.scripts.Mysecond
#RunWith(Suite.class)
#Suite.SuiteClasses([Myfirst.class, Mysecond.class])
class CustomJUnitSpecRunner {
}
And in build.gradle systemProperties System.properties, exclude the individual Myfirst.class, Mysecond.class files. So that they run only once through AllRun file.
include 'geb/groovy/AllRun/**'
exclude 'geb/groovy/scripts/**'
Groups of specs can be run using the command below, where package is a directory under src/test/groovy/specs:
mvn test -Dtest=specs.package.*
If you are relying on specs to be run in a particular order, I would reconsider your approach. If you require data in a certain state then make use of setup() and setupSpec() methods. Also consider running related tests within a single spec and run these in the order the are written by using the #Stepwise annotation.
#Stepwise
class MyStepWiseTest extends GebReportingSpec {
def myfunction(){
when:''
at mypage1
and:''
element1.text() == "mytext1"
}
def mysecondFunction() {
when:''
at mypage2
and:''
element2.text() == "mytext2"
}
}

Run specific Spock Tests without setup?

In the documentation Spock mentions these function in the section 'Fixture Methods':
def setup() {} // run before every feature method
def cleanup() {} // run after every feature method
def setupSpec() {} // run before the first feature method
def cleanupSpec() {} // run after the last feature method
I am using the setup function to initialize new objects for all tests. However some tests are supposed to test what happens when there are no other objects yet.
Because of the setup these tests fail now.
Is there any way to suspend the setup for certain functions? An annotation perhaps?
Or do I have to create a separate "setup" function and call them in each test I do. The majority of tests uses it!
You can always override value inside your test method only for that specific method. Take a look at following example:
import spock.lang.Shared
import spock.lang.Specification
class SpockDemoSpec extends Specification {
String a
String b
#Shared
String c
def setup() {
println "This method is executed before each specification"
a = "A value"
b = "B value"
}
def setupSpec() {
println "This method is executed only one time before all other specifications"
c = "C value"
}
def "A empty"() {
setup:
a = ""
when:
def result = doSomething(a)
then:
result == ""
}
def "A"() {
when:
def result = doSomething(a)
then:
result == "A VALUE"
}
def "A NullPointerException"() {
setup:
a = null
when:
def result = doSomething(a)
then:
thrown NullPointerException
}
def "C"() {
when:
def result = doSomething(c)
then:
result == 'C VALUE'
}
private String doSomething(String str) {
return str.toUpperCase()
}
}
In this example a and b are set up before each test and c is set up only once before any specification is executed (that's why it requires #Shared annotation to keep it's value between executions).
When we run this test we will see following output in the console:
This method is executed only one time before all other specifications
This method is executed before each specification
This method is executed before each specification
This method is executed before each specification
This method is executed before each specification
The general rule of thumb is that you shouldn't change c value after it is set up in setupSpec method - mostly because you don't know if which order tests are going to be executed (in case you need to guarantee the order you can annotate your class with #Stepwise annotation - Spock will run all cases in order they are defined in this case). In case of a and b you can override then at the single method level - they will get reinitialized before another test case is executed. Hope it helps.

Executing Specific Geb Tests according to environment

I have a set of Spec tests I am executing within a Grails Project.
I need to execute a certain set of Specs when I am on local, and another set of Spec when I run the pre-prod environment.
My current config is executing all my specs at the same time for both environements, which is something I want to avoid.
I have multiple environments, that I have configured in my GebConfig:
environments {
local {
baseUrl = "http://localhost:8090/myApp/login/auth"
}
pre-prod {
baseUrl = "https://preprod/myApp/login/auth"
}
}
You could use a spock config file.
Create annotations for the two types of tests - #Local and #PreProd, for example in Groovy:
import java.lang.annotation
#Retention(RetentionPolicy.RUNTIME)
#Target([ElementType.TYPE, ElementType.METHOD])
#Inherited
public #interface Local {}
Next step is to annotate your specs accordingly, for example:
#Local
class SpecificationThatRunsLocally extends GebSpec { ... }
Then create a SpockConfig.groovy file next to your GebConfig.groovy file with the following contents:
def gebEnv = System.getProperty("geb.env")
if (gebEnv) {
switch(gebEnv) {
case 'local':
runner { include Local }
break
case 'pre-prod':
runner { include PreProd }
break
}
}
EDIT: It looks like Grails is using it's own test runner which means SpockConfig.groovy is not taken into account when running specifications from Grails. If you need it to work under Grails then the you should use #IgnoreIf/#Require built-in Spock extension annotations.
First create a Closure class with the logic for when a given spec should be enabled. You could put the logic directly as a closure argument to the extension annotations but it can get annoying to copy that bit of code all over the place if you want to annotate a lot of specs.
class Local extends Closure<Boolean> {
public Local() { super(null) }
Boolean doCall() {
System.properties['geb.env'] == 'local'
}
}
class PreProd extends Closure<Boolean> {
public PreProd() { super(null) }
Boolean doCall() {
System.properties['geb.env'] == 'pre-prod'
}
}
And then annotate your specs:
#Requires(Local)
class SpecificationThatRunsLocally extends GebSpec { ... }
#Requires(PreProd)
class SpecificationThatRunsInPreProd extends GebSpec { ... }

Execute some action when Spock test fails

I'd like to execute some action when Spock test fails. Specifically, take a screenshot. Is it possible? How to do it?
Create a listener class
class ExampleListener extends AbstractRunListener {
def void error(ErrorInfo error) {
println "Actual on error logic"
}
}
then add it to each specification using implementation of IGlobalExtension that is executed for each Spec
class GlobalSpecExtension implements IGlobalExtension {
#Override
void visitSpec(SpecInfo specInfo) {
specInfo.addListener(new ExampleListener())
}
}
and finally create file named org.spockframework.runtime.extension.IGlobalExtension in your META-INF/services directory (typically it will be under src/test/resources if you are using Maven) with the full name of your IGlobalExtension implementation e.g.
com.example.tests.GlobalSpecExtension
The best way to achieve this is to write a (global or annotation-driven) Spock extension that implements and registers an AbstractRunListener. For an example, see OptimizeRunOrderExtension. For how to register a global extension, see the IGlobalExtension descriptor.
There isn't much documentation on extensions because the APIs are still subject to change. If you want to play it safe (and can live with some restrictions), you can implement a JUnit Rule instead.
One problem that you may encounter in both cases is that they don't provide access to the current spec instance. If you need this, you may have to use both an AbstractRunListener (to be notified of the failure) and an IMethodInterceptor (to get hold of the spec instance), both registered by the same extension. (Shouldn't be this hard, but that's what's currently there.)
I've managed to do it this way:
class ExampleTest extends GebSpec{
static boolean success = false
def setup(){
success = false
}
def cleanup(){
assert success == true, someAction()
}
def someAction(){
}
def "TestCase"(){
expect:
/*What you expect here*/
(success = true) != null
}
}
Before each test case "success" is set to false by the setup() method.
At the end of each test case you add the "(success = true) != null" statement. Therefore "success" will only be true if the test case has passed.
After each test case the cleanup() method will verify if "success" is true. If it isn't the method someAction() will be called.
I can't upvote or comment on user3074543's answer, but it's simpler than creating an extension. I want easy. So I shortened user*'s a little (I don't mean the 1-line methods). You can make the logic simpler by recording failure instead of success, and reduce typing with a done() helper.
class Test extends spock.lang.Specification {
def fail
def setup(){ fail = true }
def done(){ !(fail = false) }
def cleanup(){ fail && doStuffWhenFail() }
def 'test things'(){
expect:
stuff
done()
}
}

Grails integration test failing with MissingMethodException

I'm attempting to test a typical controller flow for user login. There are extended relations, as with most login systems, and the Grails documentation is completely useless. It doesn't have a single example that is actually real-world relevant for typical usage and is a feature complete example.
my test looks like this:
#TestFor(UserController)
class UserControllerTests extends GroovyTestCase {
void testLogin() {
params.login = [email: "test1#example.com", password: "123"]
controller.login()
assert "/user/main" == response.redirectUrl
}
}
The controller does:
def login() {
if (!params.login) {
return
}
println("Email " + params.login.email)
Person p = Person.findByEmail(params?.login?.email)
...
}
which fails with:
groovy.lang.MissingMethodException: No signature of method: immigration.Person.methodMissing() is applicable for argument types: () values: []
The correct data is shown in the println, but the method fails to be called.
The test suite cannot use mocks overall because there are database triggers that get called, the result of which will need to be tested.
The bizarre thing is that if I call the method directly in the test, it works, but calling it in the controller doesn't.
For an update: I regenerated the test directly from the grails command, and it added the #Test annotation:
#TestFor(UserController)
class UserControllerTests extends GroovyTestCase {
#Test
void testLogin() {
params.login = [email: "test1#example.com", password: "123"]
Person.findByEmail(params.login.email)
controller.login()
}
}
This works if I run it with
grail test-app -integration UserController
though the result isn't populated correctly - the response is empty, flash.message is null even though it should have a value, redirectedUrl is null, content body is empty, and so is view.
If I remove the #TestFor annotation, it doesn't work even in the slightest. It fails telling me that 'params' doesn't exist.
In another test, I have two methods. The first method runs, finds Person.findAllByEmail(), then the second method runs and can't find Person.findAllByEmail and crashes with a similar error - method missing.
In another weird update - it looks like the response object is sending back a redirect, but to the application baseUrl, not to the user controller at all.
Integration tests shouldn't use #TestFor. You need to create an instance of the controller in your test, and set params in that:
class UserControllerTests extends GroovyTestCase {
void testLogin() {
def controller = new UserController()
controller.params.login = [email:'test1#example.com', password:'123']
controller.login()
assert "/user/main" == controller.response.redirectedUrl
}
}
Details are in the user guide.
The TestFor annotation is used only in unit tests, since this mocks the structure. In the integration tests you have access of the full Grails environment, so there's no need for this mocks. So just remove the annotation and should work.
class UserControllerTests extends GroovyTestCase {
...
}