没有注册的仪器,运行单元测试时出现错误。

huangapple 未分类评论48阅读模式
英文:

No registered instrumentation, errors when running unit tests

问题

遇到了一个奇怪的错误在我的测试单元中。

public class GameActivityUnitTest {
    private static ActivityScenario<GameActivity> scenario;
    @BeforeClass
    public static void createLogHistory() {
        scenario = ActivityScenario.launch(GameActivity.class);
        // WHEN
        scenario.moveToState(Lifecycle.State.CREATED);
    }
    @Test
    public void testPointsChange() {
        scenario.onActivity(activity -> {
            activity.increaseCoin();
            activity.increaseCoin();
            activity.decreaseCoin();
            assertTrue(activity.coins == 1);;
        });
    }
    @Test
    public void testGamePauseStatus() {
        scenario.onActivity(activity -> {
            activity.view.getTimerController().pause();
            assertTrue(activity.view.getPauseStatus());
            activity.view.getTimerController().resume();
            assertTrue(activity.coins == 1);;
            assertFalse(activity.view.getPauseStatus());
        });
    }
}

我认为标记为 @BeforeClass 的函数被所有后续的测试单元共享。

我显示的内容给出了 "No instrumentation registered" 错误。

然而,如果我删除 @BeforeClass 方法,但在每个测试单元中重复代码。

错误消失了。

为什么会发生这种情况?

我不希望每次都重复代码,我应该怎么做?

谢谢!

英文:

I encountered a weird error in my test unit.

public class GameActivityUnitTest {
    private static ActivityScenario&lt;GameActivity&gt; scenario;
    @BeforeClass
    public static void createLogHistory() {
        scenario = ActivityScenario.launch(GameActivity.class);
        // WHEN
        scenario.moveToState(Lifecycle.State.CREATED);
    }
    @Test
    public void testPointsChange() {
        scenario.onActivity(activity -&gt; {
            activity.increaseCoin();
            activity.increaseCoin();
            activity.decreaseCoin();
            assertTrue(activity.coins == 1);;
        });
    }
    @Test
    public void testGamePauseStatus() {
        scenario.onActivity(activity -&gt; {
            activity.view.getTimerController().pause();
            assertTrue(activity.view.getPauseStatus());
            activity.view.getTimerController().resume();
            assertTrue(activity.coins == 1);;
            assertFalse(activity.view.getPauseStatus());
        });
    }
}

I think function tagged as @BeforeClass is shared by all the following test units.

What I display gives my the "No instrumentation registered" error.

However, if I delete @BeforeClass method but repeat the code in each test units.

The error disappears.

Why this happens?

And I don't want the code to be repeated every time, what should I do?

Thank you!

答案1

得分: 0

如果您想在每次运行测试之前执行一些代码,应该使用@Before注解(而不是@BeforeClass)。

这意味着您可以在createLogHistory()中轻松启动被测试的活动(现在不应该是静态的)。

英文:

If you want to execute some code before every test is run, you should use the @Before annotation (not @BeforeClass).

That means that you can easily launch the activity under test in your createLogHistory() (which should now not be static).

huangapple
  • 本文由 发表于 2020年5月4日 04:07:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定