As we continue writing .feature files and scenarios (aka. test cases) , we might end up in large # of files at some point. At that point, if we would like to slice and dice our tests meaning, we want to be able to run a certain set of scenarios based on a criteria, tags are one of many wonderful solutions that Behave gives us (Think of tags as “Test Lab” in Quality Center, where we can pull test cases from Test Plan and execute a certain set of test cases)
So what might be some criteria and rules on which we want to group test cases (statically at one time or come back later and group in a different way more dynamically). Some of the attributes on which we can tag scenarios are:
1) Smoke, Regression, Sanity etc.
2) Release type (beta, alpha, production etc.)
3) fast, slow, medium etc.
4) parallel, not_parallel
5) Really however you want to tag….
Piece of Advice
In my experience, making a decision on the criteria takes a long way in making scenarios simple (of course there is always a way to remove or replace an existing tag. We can search+replace on entire project very easily). So for example [Sanity,Smoke,Regression], [fast,slow,medium] are good tags.
So how do I tag ?
Type “@<tag_name>” at the top of Feature or Scenario – That is it!
Let’s see some examples
Tag a feature
Here we tagged @slow at the top of Feature – The tag applies automatically to all of the scenarios on the feature file
Tag a scenario
Place the tag right at the line above the scenario. Here we tagged a scenario with @sanity @beta3 (Separate multiple tags with spaces).This means that the tags [@slow @sanity @beta3], all of them apply to the scenario. So if we run any of the tags, the scenario will get picked up
Note: There are many more best practices and guidance around how tags should be used and where do they apply the best, which will be covered as part of online tutorial if you register for the course
Now Let’s execute !
Btw, we are using the same code posted in Wait Commands
Set the tag name you want to execute in ‘Run Configurations -> Default -> Behave’. Click ‘OK’. Then Run the scenario as usual by right click and run.
Set the tag name in the default profile in cucumber.yml file. And as usual right click and run
There are other ways to execute as well. Also we can combine tags using AND, OR , NOT etc. and that gives a lot of power to slice and dice the tests.