9

I want to combine both Robolectric and Cucumber (JVM).

Currently I have two classes ActivityStepdefs where two step definitions for activity management are defined.

My second class is RoActivity Where for example an activity is created from it's class name, and where Robolectric will be used.

When I run RoActivityTest using RobolectricTestRunner the test in this class passes, but when I run RunCukesTest (class for running features as junit test) the code from RoActivity is not running as part of Robolectric, i.e. RunCukesTest search for features on my project and match it with a method inside ActivityStepdefs and finally this class will call a method from RoActivity

Is possible to run test with both junit both* runners?

I'm not sure but perhaps it's possible to do something like powermock, using junit rules.

In that case for which one should I have to define the rule?

*Cucumber and Robolectric

Axxiss
  • 4,759
  • 4
  • 26
  • 45
  • Just wondering if you were able to make advancement on this? I'm attempting to do something similar and am still researching different possibilities – zabawaba99 Jun 03 '13 at 20:53
  • Not yet, if you want we can try to find a solution together – Axxiss Jun 04 '13 at 09:04
  • Take a look at https://github.com/mfellner/cucumber-jvm/tree/cucumber-android . This repo allows you to use cucumber to do integration testing on Android. I found it yesterday shortly after I had written this comment. I almost have it running just fine. If and when I get it running I'll write it as an answer with instructions for anyone else. If you get it running before me, I would encourage you to do the same :P – zabawaba99 Jun 04 '13 at 14:00
  • This project seems to be for running test on the emulator, not using Robolectric. – Axxiss Jun 04 '13 at 21:49
  • Hi Axis, did you find a solution for run test with cucumber and robolectric? – M-S May 05 '14 at 08:55
  • after one day search work.. I found a solution: https://github.com/bySabi/RoboCuke/tree/master/src/test/java/com/iguanalab/app/RoboCuke . hope it help somebody. – iptton Mar 05 '20 at 16:30

2 Answers2

1

I'am facing the same problem, after some google work, I got a solution:

@RunWith(ParameterizedRobolectricTestRunner::class)
@CucumberOptions( features = ["src/test/features/test.feature","src/test/features/others.feature"], plugin = ["pretty"])
class RunFeatures(val index: Int, val name:String) {

    companion object {
        @Parameters(name = "{1}")
        @JvmStatic
        fun features(): Collection<Array<Any>> {
            val runner = Cucumber(RunFeatures::class.java)
            Cucumber()
            val children = runner.children
            return children.mapIndexed{index, feature ->
                arrayOf(index,feature.name)
            }
        }
    }



    @Test
    fun runTest() {
        val core = JUnitCore()
        val feature = Cucumber(RunFeatures::class.java).children[index]!!
        core.addListener(object: RunListener() {
            override fun testFailure(failure: Failure?) {
                super.testFailure(failure)
                fail("$name failed:\n"+failure?.exception)
            }
        })
        val runner = Request.runner(feature)
        core.run(runner)
    }
}

but seems not an pretty solution for me, can somebody help me out these problem:

  1. must explicitly list all feature file path. but cannot use pattern such as *.feature
  2. when failed cannot know which step failed.
  3. parameter can only pass primitive type data,

I've get into cucumber source , but seems CucumberOptions inline Cucumber , I cannot pass it programmatically but can only use annotation .

iptton
  • 593
  • 1
  • 4
  • 11
  • When you post a mix of answer and a question by using answer function you are trying to achieve two different things - share your knowledge and ask for advice. Experts will **NOT** try to help you to resolve those 3 problems here because you did not create new question. Please do not hijack old question to ask new questions. – Maxim Sagaydachny Mar 08 '20 at 08:45
-1

My small 5 cents.

Cucumber is mostly used for acceptance tests (correct me if you use it for unit testing) and Robolectric is mostly used for unit testing.

As for me, it is overkill to write cucumber during TDD. And Robolectric is still not android and I would run acceptance tests on real device or at least emulator.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114