整合Robolectric和黄瓜

时间:2013-05-26 16:36:40

标签: android junit cucumber robolectric cucumber-jvm

我想结合使用Robolectric和Cucumber(JVM)。

目前我有两个类ActivityStepdefs,其中定义了活动管理的两个步骤定义。

我的第二课是RoActivity例如,活动是根据其班级名称创建的,并且将使用Robolectric。

当我使用RoActivityTest运行RobolectricTestRunner时,此类中的测试通过,但是当我运行RunCukesTest(运行功能的类作为junit test)来自RoActivity的代码时没有作为Robolectric的一部分运行,即RunCukesTest搜索我项目中的功能并将其与ActivityStepdefs内的方法匹配,最后这个类将调用来自RoActivity的方法

是否可以使用junit和* runners进行测试?

我不确定,但也许可以使用junit规则做类似powermock的事情。

在这种情况下,我应该为哪一个定义规则?

* Cucumber和Robolectric

2 个答案:

答案 0 :(得分:0)

我小5美分。

黄瓜主要用于验收测试(如果你用它进行单元测试,请纠正我),而Robolectric主要用于单元测试。

至于我,在TDD期间写黄瓜是有点过分的。而且Robolectric仍然不是android,我会在真实设备或至少模拟器上运行验收测试。

答案 1 :(得分:0)

我也遇到了同样的问题,经过Google的一些工作,我找到了解决方案:

@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)
    }
}

但是对于我来说似乎不是一个很好的解决方案,有人可以帮我解决这些问题:

  1. 必须显式列出所有要素文件路径。但不能使用* .feature
  2. 之类的模式
  3. 失败时不知道哪个步骤失败。
  4. 参数只能传递原始类型的数据,

我进入了黄瓜源,但是似乎CucumberOptions内联了Cucumber,我无法以编程方式传递它,而只能使用注释。