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

@WithJunitRule executes rule for every scenario #118

Closed
ThomasRichtsfeld opened this issue Jul 13, 2023 · 11 comments
Closed

@WithJunitRule executes rule for every scenario #118

ThomasRichtsfeld opened this issue Jul 13, 2023 · 11 comments

Comments

@ThomasRichtsfeld
Copy link

👓 What did you see?

When using a class annotated with @WithJunitRule that contains a rule it gets executed for each scenario.

✅ What did you expect to see?

I only expect the step classes that "inject" the rule in the constructor to execute the rule

📦 Which tool/library version are you using?

We are running instrumented tests on Android using test orchestrator

"io.cucumber:cucumber-android" version 4.10.0

🔬 How could we reproduce it?

Rule example:

@WithJunitRule
internal class GrantCameraPermissionRuleHolder {

    @get:Rule
    val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.CAMERA)
}

This rule is not injected anywhere, but it still gets executed for each scenario

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

This is correct behaviour if I correctly understand your issue. Rule is executed every scenario the same as in normal Junit tests. Scenario == single junit test case. It doesn't matter where you inject test rule. Steps classes does not impose any lifecycle behaviour of test. You can have 1 scenario with 10 steps and every step can be implemented in different step class which means that you will have 10 steps classes objects created for every scenario. WithJunitRule is also created for every scenario no matter if you inject it somewhere or not because rule can setup some environment which is needed for test but not necesessarily has to be directly used in test code

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

So in other words in Cucumber everything which is in glue package is considered to be used in scenario execution while in Junit it is limitted to test class which can cause some confusions but this is how Cucumber works

@ThomasRichtsfeld
Copy link
Author

Yep, that's right. I understand that.
Is it then even possible to use Junit Rules that are applied only to specific step definition classes?
Let's say I want to create a test user with a rule before 2 of my 10 scenarios. How would that work with a Junit Rule?

This behavior was not fully clear to me when reading the documentation. In the demo app, you inject the scenario holder for compose into one of the step classes which made me think that this is how it works

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

Demo example is very simple however as lifecycle of scenario execution ia a bit different there is no correlation between injecting rules and executing them. Currently is not possible to achieve what you want but in general this should be done by specifing some filter for scenario similarily as in @After annotation:

obraz

But as this is not currently implemented for WithJunitRule so it will not work

@ThomasRichtsfeld
Copy link
Author

Alright, thanks. Do you think there will ever be a way to use rules for single scenarios? For the user creation case, I could call a static function in the step definition which is no problem, but for rules like GrantPermissionRule that is defined by Android it is impossible to use them only for certain scenarios.

Can you give an example of what you mean by "some filter for scenario similar as in @After annotation"?

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

AS mentioned in @After annotation documentation a filter is a tag expression which matches particular scenario. So you can add tag above scenario:

@ScenarioWithGrantPermission
Scenario: Lorem ipsum

and then

@After("@ScenarioWithGrantPermission")
fun after() {...}

and this after ,ethod will be executed only for scenarios with @ScenarioWithGrantPermission tags. For tag expressions please read cucumber documentation https://cucumber.io/docs/cucumber/api/?lang=java#tag-expressions.

So similar mechasim could be implemented for WithJunitRule

@ThomasRichtsfeld
Copy link
Author

It would be great if there is scenario support for the WithJunitRule.
As this is then more like a feature request, should we close the thread now?

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

We can keep it open until PR will be created

@ThomasRichtsfeld
Copy link
Author

Thanks a lot for the fast response @lsuski
Let me know if I can support in any way

@lsuski
Copy link
Contributor

lsuski commented Jul 13, 2023

Sure, you can support by creating a PR with tag expression support in WithJunitRule :)

@lsuski
Copy link
Contributor

lsuski commented Sep 22, 2023

With version 7,14,0 (not released yet) you will be able to specify tag expression in @WithJunitRule("@MyTag")

@lsuski lsuski closed this as completed Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants