【问题标题】:Espresso android -- Evoke assert? (NOT a view assert)Espresso android——唤起断言? (不是视图断言)
【发布时间】:2015-03-14 00:33:58
【问题描述】:

我想做一个简单的测试。我只想让我的 espresso 测试脚本验证我不在生产中。如果我在生产中进行购买,就会发生坏事,更不用说批量购买了。 我知道在 Java 中你需要添加一个 -ae 来运行断言。在 android espresso 测试中,这似乎并不那么简单。我将把这段代码交给测试人员,所以如果它在生产中,我真的需要它失败。 (显然我会将它包装在一个 IF 中,但我希望它更丑陋——你搞砸了——有点东西。)

public class PurchaseTest  extends BaseFooTest<HomeActivity> //ActivityInstrumentationTestCase2<LoginRegisterActivity>
{

final static String TAG = "PurchaseTest";
static final String PROD_URL = "https://api.foobar.com";
public PurchaseTest()
{
    super(HomeActivity.class);
}

public void test()
{

    System.out.println(fooApplication.hostUrl);
    assert fooApplication.hostUrl.equalsIgnoreCase(PROD_URL) == false;
    assert fooApplication.hostUrl.equalsIgnoreCase(PROD_URL) == true;
//       No assert! Not being read then!

}

/////////////////////////// boss mans代码,班级正在扩展,我认为这不重要,但包括了basefootest basefootest把某人弄糊涂了。

   public class BaseFooTest<T extends Activity> extends ActivityInstrumentationTestCase2
   {
   public BaseFooTest( Class<T> activityClass )
   {
       super( activityClass );
   }

   @Override
   public void setUp() throws Exception
   {
        super.setUp();
        getActivity();
        tryClickOk();
    }

    protected ViewAssertion isDisplayed()
    {
        return ViewAssertions.matches( ViewMatchers.isDisplayed() );
    }

    protected void tryClickOk()
    {
        try
        {
            onView( withText( "OK" ) ).perform( click() );
        }
        catch ( NoMatchingViewException e )
        {
        // Eat it
        // System.out.print( e );
        }

    }
}

【问题讨论】:

  • assertTrue 等通常用于 JUnit 测试,而不是普通的 java 断言。有什么理由不想使用它们?
  • 不,espresso 的介绍并没有告诉我像 jUnit 一样对待它,所以这就是我需要知道的。让它成为一个答案,我会投票赞成。

标签: android android-espresso


【解决方案1】:

它是possible to enable keyword asserts,但它需要一个手动步骤,并且在测试代码中使用关键字断言是不常见的。

最好像在单元测试中那样使用 JUnit 方法(assertTrue 等)。

【讨论】:

  • 谢谢!我想知道为什么,Kotlin,我的 assert(condition) { "lazy message" } 在其中一个失败时不会停止测试!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多