Search code examples
pythontagspython-behave

Tag different sets of example in behave test differently


I have a springboot service that can be booted with different spring.profiles.active. The service was initially created to start with spring.profiles.active=profile1. Someone then added behave test/scenarios for the service.

Later, I added another profile to the service and now I want to run the behave tests for both profiles. So, first run behave tests when the service is started with spring.profiles.active=profile1 and then run the tests again with service started with spring.profiles.active=profile2.

I can run behave tests twice and use environment variables to control the spring profiles settings. I can also tag my scenarios differently for the two profiles like

@profile1
Scenario Outline: test something in profile1
  Given: blah
  ...


@profile2
Scenario Outline: test something in profile2
  Given: blah
  ...

Then run behave tests twice as
>> behave --tags=profile1
>> behave --tags=profile2

However, there is a lot of scenarios that are common across both profiles and it does not make sense to repeat those with different tags. For example,

Scenario Outline: test that a user is recommended at least 10 items
  Given a user <userId>
  when recommendations are generated
  then at least 10 items are returned in the recommendations

Examples:
|userId|
|123456| # I want to use this userId for profile1
|234567| # and this one too for profile1
|987654| # but this one and the following for profile2
|876543|

A similar post provides a solution like

Scenario Outline: test that a user is recommended at least 10 items
  Given a user <userId>
  when recommendations are generated
  then at least 10 items are returned in the recommendations

Examples:
|userId|
|123456| # I want to use this userId for profile1
|234567| # and this one too for profile1

Examples:
|userId|
|987654| # but this one and the following for profile2
|876543|

and then use `row2.4` etc to call specific row of examples for the second profile.

This, however, does not appear elegant to me (what if I disable or rearrange the examples later?).

My question is, is there a way to tag specific example rows?

So, something along the line of

Examples: @profile1
|userId|
|123456| # I want to use this userId for profile1
|234567| # and this one too for profile1

Examples: @profile2
|userId|
|987654| # but this one and the following for profile2
|876543|

Solution

  • My question is, is there a way to tag specific example rows?

    You were really close.

    @profile1
    Examples: 
    |userId|
    |123456| # I want to use this userId for profile1
    |234567| # and this one too for profile1
    
    @profile2
    Examples: 
    |userId|
    |987654| # but this one and the following for profile2
    |876543|
    

    Although the appendix has a suggestion for userdata implementation that might be useful for your example scenario. Or you might want to look at active tag matcher @use.with_profile=profile1 below that.