0

在每次测试执行之后,junit 进入下一个测试。每次测试执行后活动都会自动关闭。

测试功能时可以,但测试视图时不行。我想在 junit 执行下一个测试之前在屏幕上看到执行结果。

所以,我想在测试之间做一些小的延迟。


我有一种方法可以做到:

public void testMyView() throws InterruptedException {
    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // viewOperations...
        }
    });
    Thread.sleep(3000);
}

但我不喜欢这种方式,因为这段代码很脏(尤其是当我有很多这样的测试时)。


另一种方法似乎要好得多:

@UiThreadTest
public void testHandleEmptyAlphabetList() throws InterruptedException {
// view operations
    Thread.sleep(3000);
}

但是:它会阻塞 UI 线程,但不会阻塞测试线程。我需要阻止一个测试线程。

有没有办法Thread.sleep(3000);在测试线程中单独运行这段代码?


那么,在测试之间产生线程延迟的最佳方法是什么?

ps 请不要向我推荐额外的测试框架,我想使用嵌入工具来解决我的问题。谢谢你。

4

2 回答 2

-1

I suggest ditching this cumbersome way of unit testing and using Robolectric, so your tests run in the JVM and you don't have to deal with all this nasty setup. You'll be pleasantly surprised on how quickly your View testing runs, and that you don't need to run an emulator!

于 2012-12-26T05:02:59.157 回答
-2

尝试使用Robotium进行需要真正的 android 运行的测试。

solo = new Solo(getInstrumentation(), getActivity());
[...]
if (failure) {
    solo.takeScreenshot();
} 

这会将屏幕截图保存到您的 SD 卡(需要 WRITE_EXTERNAL_STORAGE 权限)

或者,如果您不想使用其他测试框架(尽管我建议您看一下它),只需窃取那里用于截屏的代码

于 2013-01-04T09:57:24.023 回答