当一个自动化测试结束控制 - 居preSSO [英] Controlling when an automation test ends - Espresso

查看:123
本文介绍了当一个自动化测试结束控制 - 居preSSO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的情况我测试。我使用的长者preSSO写我的测试。我知道居preSSO和InstrumentationTestCase并不意味着这样做。

I have an unusual situation I am testing. I am using Espresso to write my tests. I know Espresso and InstrumentationTestCase is not meant to do this.

我有我在类,会通知我有一定的价值的变化之一创建一个监听器。我用的是听者在我的测试套件。

I have a Listener I created in one of my classes that will notify me of a change of a certain value. I use the listener in my test suite.

当我从听众中获得的价值,我需要断言值改变这样的。

When I get the value from the listener, I need to assert the value was changed as such.

我的问题是,在测试结束前,我将获得价值形态的听众。

    private void sendSpeedChanges() {
    setStaticSpeed(new Random().nextInt(10) + 2);
    try {
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                consoleActivity.onSpeedChanged(getStaticSpeed(), false);
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }

}

private void createSpeedDelegate() {
    EspressoMachineValues.setOnSpeedChangeListener(new EspressoMachineValues.OnSpeedChangeListener() {
        @Override
        public void onSpeedChanged(double speed) {
            //assert speed is correct.
            assertTrue(getStaticSpeed() == speed);
        }
    });

}

这是两种方法,我使用。在 createSpeed​​Delegate()是在开始通话。然后,我打电话 sendSpeed​​Changes 。我需要做的时候这种X量。

These are the two methods I am using. The createSpeedDelegate() is call at the very beginning. Then I call sendSpeedChanges. I need to do this X-amount of times.


  • 大约需要200毫秒来检索信息(平均)。

  • 我不能叫 sendSpeed​​Changes(),直到我在 onSpeed​​Change检查值()

  • 我不能使用视频下载(); ,因为听众​​是主线程。

  • It takes about 200 milliseconds to retrieve info (On average).
  • I can't call sendSpeedChanges() until I have checked the value in onSpeedChange()
  • I cannot use Thread.sleep(); because the listener is on the main thread.

我试图添加 getInstrumentation()等(2000年); getInstrumentation()waitForIdleSync (); 很显然,无论是工作

I have tried adding a getInstrumentation().wait(2000); and also getInstrumentation().waitForIdleSync(); Obviously, neither work.

在一个完美的世界,我会做这样的:

In a perfect world, I would do this:

   for (int i = 0; i < 42; i++) {
        sendSpeedChanges();
        i++;
    }

不过,这不会等待检查的价值。如果我等待值,测试运行以为所有测试完成并终止。

But, this will not wait for the value to be checked. And if I do wait for the value, the test runner thinks all tests are done and terminates.

我的提问是,会有当测试退出来控制的一种方式?虽然我的测试中的显示的工作要做。

My question is, would there be a way to control when the test quits? Even though my tests appear to be done.

推荐答案

在您的测试,你将需要创建,保持该测试,只要你希望它运行的运行控制语句。

In your test you will need to create a control statement that keeps that test running as long as you want it to be run.

while(myTestNeedsToRun) {
    if(speedChangeReceived) {
        sendSpeedChange();
        speedChangeReceived = false;
    }
}

private void createSpeedDelegate() {
   EspressoMachineValues.setOnSpeedChangeListener(new EspressoMachineValues.OnSpeedChangeListener() {
    @Override
    public void onSpeedChanged(double speed) {
        //assert speed is correct.
        assertTrue(getStaticSpeed() == speed);
        speedChangedReceived = true;
    }
});

一旦你决定完成运行测试只需设置 myTestNeedsToRun = FALSE ,然后测试将结束。

这篇关于当一个自动化测试结束控制 - 居preSSO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆