Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide a mechanism to log spock-blocks on the fly #645

Closed
raghukiranp opened this issue Sep 15, 2016 · 5 comments
Closed

provide a mechanism to log spock-blocks on the fly #645

raghukiranp opened this issue Sep 15, 2016 · 5 comments

Comments

@raghukiranp
Copy link

raghukiranp commented Sep 15, 2016

Issue description

Spock should provide a mechanism/option to let users log/report spock block labels on the fly when the test execution is happening.
Having the BDD reports in logs can be a good addition and provides a context of what is happening when we analyze really huge console logs in test runs.

Context for this enhacement

We are working on a huge enterprise application Integration and are leveraging spock to write tests
Our specifications include verifying contents across multiple apps and validate the contents using Spock.

How to reproduce / Show me how it should look?

This is how a simple test case of ours looks like

def 'Test interacting with two different apps and having too much action and loggin in Helpers'() {

    when: 'User Makes some changes in admin Application'
        def res = AdminHelper.createNewProductInCatalog("MyAwesomeProduct"); // Performs a series of actions and logs stuff

    then: 'Response message from admin is as expected'
        res.status == true

    when: 'user Publishes the changes to Production Server'
        def prodResponse = AdminHelper.publishChangesToProduction(); //Perfors a series of actions and logging 

    then: 'production response should have a success message and 0 Status code'
        prodResponse.message == "success"
        prodResponse.statusCode == 0

    when: 'user connects to Production Server and gets the CatalogContent'
        def catalogContent = ProductionHelper.getCatalogContent(); // Peroforms series of actions and logging

    then: 'User validates if the product he created is in the Catalog'
        catalogContent.product[0].name = "MyAwesomeProduct"
}

This is the kind of logging I am trying to Achieve

when: 'User Makes some changes in admin Application'
    GET : http://myAdminServer.company.com/rest/catalog/products
    HTTP/1.1 200 OK
    POST : http://myAdminServer.company.com/rest/catalog/products/MyAwesomeProduct
    HTTP/1.1 200 OK
then: 'Response message from admin is as expected'
    <<ALL VALIDATIONS PASSED>>
    <<Parameter to log the statement / Success message here? >>
when: 'user Publishes the changes to Production Server'
    POST : http://myAdminServer.company.com/rest/publish
    HTTP/1.1 200 OK
then: 'production response should have a success message and 0 Status code'
    <<ALL VALIDATIONS PASSED>>
when: 'user connects to Production Server and gets the CatalogContent'
    GET : http://www.company.com/rest/products
    HTTP/1.1 200 OK
then: 'User validates if the product he created is in the Catalog'
    <<ALL VALIDATIONS PASSED>>

What have I done before raising this issue

I tried creating my own Spock extension with mySpockRunListener registered, I customize logging and reporting and got below log.

  • Below are my Observations
    • There is no beforeBlock and afterBlock contracts defined in AbstractRunListener class, effectively binding us to use beforeFeature and afterFeature methods to do things and access blocks.
    • I tried logging BlockInfo in beforeFeature method and this is how the output is turning out to which is not of much help when analyzing results that log a lot of info.
    • Is it possible to modify SpecParser.java class to treat every Block as a method too in the ObjectModel and provide a beforeBlock and afterBlock contract to end users to have better Reporting logging and Documenting capabilities?
when: 'User Makes some changes in admin Application'
then: 'Response message from admin is as expected'
when: 'user Publishes the changes to Production Server'
then: 'production response should have a success message and 0 Status code'
when: 'user connects to Production Server and gets the CatalogContent'
then: 'User validates if the product he created is in the Catalog'
GET : http://myAdminServer.company.com/rest/catalog/products
HTTP/1.1 200 OK
POST : http://myAdminServer.company.com/rest/catalog/products/MyAwesomeProduct
HTTP/1.1 200 OK
POST : http://myAdminServer.company.com/rest/publish
HTTP/1.1 200 OK
GET : http://www.company.com/rest/products
HTTP/1.1 200 OK
@rLitto
Copy link

rLitto commented Jan 31, 2017

No feedback on it?

@raghukiranp
Copy link
Author

I have not received any response on this yet... Waiting for someone to actually comment in this.

@kriegaex
Copy link
Contributor

FYI, if you need a workaround until PR #111 is available as a feature, see my StackOverflow answer which I am quoting here. By using src/test/resources/SpockConfig.groovy and declaring a printing method named _ therein as a mixin to Specification we can achieve the method is also available in subclasses such as GebSpec, GebReportingSpec or your own (base) classes derived from either of them.

import spock.lang.Specification

class LabelPrinter {
  def _(def message) {
    println message
    true
  }
}

Specification.mixin LabelPrinter

Now if we have a spec like this (please note the unobtrusive underscores, e.g. given:_ "blah"):

package de.scrum_master.testing

import spock.lang.Specification

class MySpockTest extends Specification {
  def "interaction"() {
    given:_ "My given comment"
    def foo = 11
    def bar = foo + 4
    println "blah"

    expect:_ "My expect comment"
    interaction {
      foo = 2
      bar = 5
      true
    }
    println "foo"
    foo * bar == 10
    foo + bar == 7

    and:_ "My and comment"
    true
  }
}

We get a console log like this:

My given comment
blah
My expect comment
foo
My and comment

@leonard84
Copy link
Member

Since this issue is just another aspect of #538 I'm closing it so that the discussion is not spread out in multiple issues.

@kriegaex
Copy link
Contributor

Very good, thanks. No more need for me cross-posting. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants