How to get log messages to display in Spock Reports? - spock

I am using the Spock Reports extension in a Geb test. I am trying to find if there is any way to get the logging messages to display in the Spock report.
I have something roughly like this:
#Slf4j
class SpockReportExample extends GebReportingSpec {
def expectedVar = "5"
when: "I click the button."
button.click()
then: "The new value is displayed."
def value = formElement.value()
value==expectedValue
log.info("The new value is $value")
}
I'd like to see the the log stating the actual value to be output in my spock report, but I can't find a way. I've tried sending the log at info, warning, debug, error, and trace levels but no luck. Is this possible?

If you happen to use Athaydes Spock Reports you can write messages to the report by using
reportInfo "This is a message"
See https://github.com/renatoathaydes/spock-reports#how-to-use-it
This is possible since V1.4.0

Don't use #Slf4j just use println. spock, junit, and so on capture standard out and add it to the test results.

Related

How add Reporter.log() to log on Jenkins?

How can I add Reporter.log() on Jenkins log?
I write my test in java and used TestNG.
I try import:
import org.testng.Reporter;
next write method:
Reporter.log("Click on button next");
click(btnNext);
return this;
}
and write test:
public void shouldPageChangeAfterClick(){
mainPage
.launch()
.clickOnBtnNextPage()
.assertThatPageIsCorrect();
}
Next I run test on Jenkins from pom.xml, test is failed, but I don't see Reporter.log() in log on Jenkins.
What should I do to display Reporte.log() on Jenkins?
Reporter.log() method is to log the passed string to the HTML report. It will not print in any console or log file. You have to use different logger api to get the same in Jenkins console. Log4j will be helpful in this case.

zephyr how to update test description

I have test running in Jenkins and I have integrated the test to display in JIRA using Zephyr
The test looks something like this -
#Test(enabled=true, description="check for OS Anomaly ")
public void testOS4() {
....
}
and in the JIRA why dont I see any test description - the test description shows as "Creating the Test via Jenkins" (pls see the attachment). Does anybody know whats wrong here?
Just verify the JSON you are PUTTING to Zephyr URL. If there is a "Description" Key with value "Created the Test via Jenkins", then you can update the key value in the JSON object. something like
obj.put("status", status );
obj.put("summary", summary);
obj.put("description",description);

How to take screenshots in a module in geb

Hi we run Geb tests on the Spock framework. I am trying to take a screenshot in a module using report "Screenshot". It does not recognize the report function as it does on a Spec. How should I go about taking screenshots in a module.
Here is a example code which is in a module.
try{
$(By.xpath("//button[#ng-click=\"ok()\"]")).click()
}
catch (Throwable t){
failures.add("\n Could not click on the Ok button after the Ticket created successfully message appeared")
report "Failure"
}
The report() method is available on Browser class and an instance of that class is available as browser property inside of modules, so you can call it from within a module using:
browser.report("Failure")
Use the parent class GebReportingSpec (instead of GebSpec) to create a report of all your tests.
When you only want a screenshot of failing tests, use the config value reportOnTestFailureOnly=true in your GebConfig.groovy

useOriginalMessage() and multiple 'from' nodes

I'm using the Grails routing plugin which allows defining Camel routes with a Groovy DSL syntax very similar to the Java DLS syntax.
Suppose I have the following RouteBuilder:
class MyRoute extends RouteBuilder {
from('activemq:route1')
.to('someProcessor1')
.to('direct:route2')
from('direct:route2')
.to('someProcessor2')
onException(Throwable.class).useOriginalMessage().handled(true)
.to('activemq:route.failed')
}
If I have a message that starts at activemq:route1, then moves through direct:route2 but fails in the someProcessor2, then I end up with the message as it started at activemq:route1 in my activemq:route.failed queue... but that's not what I want. If I have a failure in someProcessor2, I want the message as it started at direct:route2 (and likewise, if I have a failure in someProcessor1, I want the activemq:route1 message in my failed queue).
Is there any Apache Camel feature that allows me to "reset" the original message at the beginning of a RouteDefintion (i.e. from(<uri>))?
use something besides direct: to join your routes (seda, vm, activemq) and it will behave as you suggested...otherwise, you can also explicitly preserve the relevant state of the message in a header and restore it in the onException clause, etc.

"Test a little, code a little" in Grails project, using Easyb or Spock, in IntelliJ

My original issue is described perfectly by this post: I want to follow TDD:
write a small test
watch it fail
write just enough code to make it succeed
watch it succeed
repeat
I am working on a Grails project in IntelliJ. If all I want is to write normal JUnit tests, the above post solves everything:
Head to /test/unit
Put some test code in a "class Xyz extends GroovyTestCase" class
Hit Shift F10
JUnit report pops up within a second or two
The problem is that I would like to use one of the very cool "describe-in-english" testing setups, like Easyb or Spock.
What do I do? It would be magic to just start with the auto-generated Test class Grails makes for me, then cram Spock stuff into it. Obviously I can't use "extends" twice. Does this give the gist of what I'm trying to do though?
class Xyz extends GroovyTestCase extends spock.lang.Specification {
//void testSomething() {
// fail "Implement me"
//}
def "length of Spock's and his friends' names"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
}
}
Extend spock classes, not groovy's. You can choose from UnitSpec, ControllerSpec, IntegrationSpec and others as listed in source code. Spock will take care of the rest.
-Use "grails install-templates"
-Change the Tests templates to something along the lines of:
#artifact.package#
import grails.test.mixin.*
import org.junit.*
import spock.lang.Specification
#TestFor(#artifact.testclass#)
class #artifact.name# extends Specification {
}
-Write the test code in Spock (or normal JUnit code)
-IntelliJ->Run->Edit Configurations. Add New Configuration of the JUnit type. Test kind: <All in package/All in UnitTest directory/All in one UnitTest class/etc.>
-(Shortcut: Cursor over method name, or class name->ctrl-shift-F10)
On my original question:
In retrospect, I was getting hung-up on the "run all in directory" part of that blog post. Current IntelliJ DOES let you run all JUnit tests in a directory, or the entire project.
Once I understood that, the next step was to realize that Spock tests ARE JUnit tests. Make a "class Xyz extends Specification {}" class in the test/unit directory, fill it with Spock code, and Run as... JUnit. Magic!

Resources