Discussion:
[Cucumber] Get all tags from a .feature file
Simon P.
2015-04-09 15:16:47 UTC
Permalink
Does anyone know if there is a way to get all the tags from a .feature file
and use them in my .java step definition file?

I saw there are some gherkin.formatter.model.Tag and
gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be
used, but how do I reference the specific Scenario I want?

If that's not possible, do I need to convert my Gherkin file into json
first?

I've searched and haven't found anything about this.

example:
@HelloWorld @alive
Scenario: Say hello
When I say hello!
Given the world is around
Then the world says hi!

stepdefinition.java

// TODO: Is there a way to get all the tags in a feature file?
@Before("@alive")
public void beforeScenario()
{
// print a list of tags in this .feature file
}
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Eric Kessler
2015-04-09 17:05:03 UTC
Permalink
I would like to think that you would have access to some kind of scenario
object in your 'before' hook and that from that object you could get access
to the feature object and from there get access to all of the other
scenario/outline objects, thus finally allowing you to get access to their
individual tags. However, I'm only familiar with Ruby's implementation of
Cucumber and maybe the Java implementation doesn't expose as much as Ruby
does at runtime. That being said, I bet that I could code up *something *that
will accomplish what you want but it likely wouldn't be as efficient as an
inspection of the runtime objects.
Post by Simon P.
Does anyone know if there is a way to get all the tags from a .feature
file and use them in my .java step definition file?
I saw there are some gherkin.formatter.model.Tag and
gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be
used, but how do I reference the specific Scenario I want?
If that's not possible, do I need to convert my Gherkin file into json
first?
I've searched and haven't found anything about this.
@HelloWorld @alive
Scenario: Say hello
When I say hello!
Given the world is around
Then the world says hi!
stepdefinition.java
// TODO: Is there a way to get all the tags in a feature file?
@Before("@alive")
public void beforeScenario()
{
// print a list of tags in this .feature file
}
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Aslak Hellesøy
2015-04-09 17:08:42 UTC
Permalink
Does anyone know if there is a way to get all the tags from a .feature file and use them in my .java step definition file?
What dou you need it for?
I saw there are some gherkin.formatter.model.Tag and gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be used, but how do I reference the specific Scenario I want?
If that's not possible, do I need to convert my Gherkin file into json first?
I've searched and haven't found anything about this.
@HelloWorld @alive
Scenario: Say hello
When I say hello!
Given the world is around
Then the world says hi!
stepdefinition.java
// TODO: Is there a way to get all the tags in a feature file?
@Before("@alive")
public void beforeScenario()
{
// print a list of tags in this .feature file
}
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Simon P.
2015-04-09 17:28:48 UTC
Permalink
Hey,
Thanks so much for the replies.

I want to tag each of the test case ids in the Gherkin .feature file.

In the .java step definition file, I want to pass in all the tags that
exist in my .feature file. Instead of having to include each of them
separately, otherwise I run the risk of having to make sure to update the
tags in the .java file anytime I update the .feature file.

I then want to print out all the tags (test case ids) and their requirement
descriptions to the test report.

example:
@HelloWorld @alive @happy @sad
Scenario: Say hello
When I say hello!
Given the world is around
Then the world says hi!

stepdefinition.java

// TODO: Is there a way to get all the tags in a feature file?
@Before(<a list of tags>)
public void beforeScenario()
{
// print a list of tags in this .feature file

}


I found some info below but I haven't found an example of how those tags
are used in the step definition file.
https://github.com/cucumber/cucumber/wiki/Tags

Tags are also a great way to “link” your Cucumber features to other
documents. For example, if you have to deal with old school requirements in
a different system (Word, Excel, a wiki) you can refer to numbers:

@BJ-x98.77 @BJ-z12.33
Feature: Convert transaction
Post by Simon P.
Does anyone know if there is a way to get all the tags from a .feature
file and use them in my .java step definition file?
What dou you need it for?
I saw there are some gherkin.formatter.model.Tag and
gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be
used, but how do I reference the specific Scenario I want?
If that's not possible, do I need to convert my Gherkin file into json first?
I've searched and haven't found anything about this.
@HelloWorld @alive
Scenario: Say hello
When I say hello!
Given the world is around
Then the world says hi!
stepdefinition.java
// TODO: Is there a way to get all the tags in a feature file?
@Before("@alive")
public void beforeScenario()
{
// print a list of tags in this .feature file
}
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Eric Kessler
2015-04-09 17:54:54 UTC
Permalink
"I want to tag each of the test case ids in the Gherkin .feature file."

Considering that you just said almost every keyword in a use case that I am
very familiar with, either I am misinterpreting what your end goal is or I
have written a library that handles your use case exactly. ;)


So you wish to add tags to a bunch of existing gherkin tests in your
codebase?
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Simon P.
2015-04-10 07:14:44 UTC
Permalink
Yeah that's right, each of the 'requirementIds' would be a tag, and that
way I'd know what requirements are needed for that test case.

Do you a way to get a Scenario object so I can get all the tags for a
scenario?

I saw this page as well.

https://pythonhosted.org/behave/gherkin.html

which has an example of using feature.tags, but I'm not sure how to get
that feature object.
Post by Eric Kessler
"I want to tag each of the test case ids in the Gherkin .feature file."
Considering that you just said almost every keyword in a use case that I
am very familiar with, either I am misinterpreting what your end goal is or
I have written a library that handles your use case exactly. ;)
So you wish to add tags to a bunch of existing gherkin tests in your
codebase?
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Björn Rasmusson
2015-04-10 10:55:27 UTC
Permalink
Post by Simon P.
Yeah that's right, each of the 'requirementIds' would be a tag, and that
way I'd know what requirements are needed for that test case.
Do you a way to get a Scenario object so I can get all the tags for a
scenario?
You can get the tags that has been applied to a scenario from a before hook
in Cucumber-JVM this way:

public void before(cucumber.api.Scenario scenario){
for(String tag : scenario.getSourceTagNames()){
System.out.print("Tag: " + tag);
}
}


Best regards
Björn
Post by Simon P.
I saw this page as well.
https://pythonhosted.org/behave/gherkin.html
which has an example of using feature.tags, but I'm not sure how to get
that feature object.
Post by Eric Kessler
"I want to tag each of the test case ids in the Gherkin .feature file."
Considering that you just said almost every keyword in a use case that I
am very familiar with, either I am misinterpreting what your end goal is or
I have written a library that handles your use case exactly. ;)
So you wish to add tags to a bunch of existing gherkin tests in your
codebase?
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Simon P.
2015-04-10 11:11:27 UTC
Permalink
Thanks, I figured out that I could use cucumber.api.Scenario this morning
and it's working great!
Post by Björn Rasmusson
Post by Simon P.
Yeah that's right, each of the 'requirementIds' would be a tag, and that
way I'd know what requirements are needed for that test case.
Do you a way to get a Scenario object so I can get all the tags for a
scenario?
You can get the tags that has been applied to a scenario from a before
public void before(cucumber.api.Scenario scenario){
for(String tag : scenario.getSourceTagNames()){
System.out.print("Tag: " + tag);
}
}
Best regards
Björn
Post by Simon P.
I saw this page as well.
https://pythonhosted.org/behave/gherkin.html
which has an example of using feature.tags, but I'm not sure how to get
that feature object.
Post by Eric Kessler
"I want to tag each of the test case ids in the Gherkin .feature file."
Considering that you just said almost every keyword in a use case that I
am very familiar with, either I am misinterpreting what your end goal is or
I have written a library that handles your use case exactly. ;)
So you wish to add tags to a bunch of existing gherkin tests in your
codebase?
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...