Closed buptfarmer closed 3 years ago
Cucumber @Before
and @After
works globally in opposite to junit ones which works locally for test class only. It means that before/after each scenario there will be invoked every @Before
/@After
annotated method in glue package. If you added new annotated methods then it requires different mechanism for managing activity, rule cannot be used. In my company we don't use Activity rule. Instrumentation can be obtained via InstrumentationRegistry
and activity can be started with without junit rule by startActivitySync
. Each activity started during test can be found inActivityLifecycleRegistry
According to your 2 question. There is no such code. In cucumber you provide glue package which is used for scanning all classes containing cucumber steps or hook annotations
For single scenario you can have steps defined in many classes - cucumber instantiate those classes and invokes specific methods. Sample is very simple, maybe to simple. I think that it would be worth to split CalculatorActivitySteps
in to 2 classes to show how it works. Activities should not be started in before hook but in concrete step, e.g When I start calculator
I would also give up finishing activity in after hook because it is naive. Tested activity can start another one which would not be finished then and leaks to next scenario. Start activity with clear top flag which should be sufficient
@lsuski Thanks for your detailed reply. I get it that
I am going to make some modification the see the result.
but one question remain
com.mytest.test.sceneAdd
and feature/sceneAdd
Am I right? You can have many methods annotated with @After
/@Before
annotation but you have to keep in mind that all will be executed
As cucumber searches for first class annotated with @CucumberOptions
in package taken form InstrumentationRegistry.getInstrumentation().getContext().getPackageName()
you can have 2 classes in this package - one for local development build (add it to .gitignore) and second one for CI build (in git). I usually name first one a Atest
and second one CucumberConfiguration
. As first one is for letter 'A' it will be taken by cucumber before this for letter C
I spent some time getting familiar with example as well as cucumber engine and I can agree the fact that all @After/@Before
calls will be executed was totally not clear to me. I spent quite a few hours trying to figure out why espresso kept complaining that app is not idle. Only to learn that I was launching two same activities on top of each other, so espresso 'did not see' the second one.
With that said, I was evaluating feasibility of migrating our rather big test infra (~300 tests) to cucumber. I noticed there are some 'rules' (ie constraints) in runner that were not clear to me. I'm mentioning that here, as I believe the fact that all @Before
s are run before each scenario is one of them. Can I create a separate issue to mention and hopefully shed some lights on specific use-cases, where current runner is incompatible with our needs? Maybe slack community will be better to have a chat or there are some materials you could point me towards? Thanks!
@dbarwacz Cucumbr for Android works in most cases the same as cucumber for java so this is rather issue for cucumber-java. @After/@Before
should be declared in separate classes dedicated for hooks. For very simple use case it can be declared in same class as steps but I never do that this way
To clarify; while they look alike; both use annotated methods, both use classes. There is a fundamental difference between JUnit and Cucumber. This difference tends to trip people up the first time round.
A test in JUnit is a single @Test
annotated method executed against an instance of the test class.
A test in Cucumber is a Scenario which is executed by invoking multiple @Given/When/Then
annotated methods against a collection of instances, one for each class with an annotation.
Because all classes with annotated methods are part of the test context, it is only logical that all hooks are also executed. They are all in scope.
Thank you for clarifications. This makes total sense now!
Firstly, I successfully run the example test project, which passed all the test cases. so I began to make some modification to understand it. I write a new file ChansonSteps.java based on the CalculatorActivitySteps.java, modified steps from "I have a CalculatorActivity" to "Chanson have a CalculatorActivity" and etc. but it fail to run the test cases after the ChansonSteps.java added. Here is the ChansonSteps.java I added
here is the error.
so, I want to figure out
Thanks for anyone who might help.