2014-10-07 5 views
17

내 설정 : - 전화와 안드로이드 앱 및 태블릿 버전 - 나는안드로이드 에스프레소 테스트

(buildagent에서 전화 만 전화 버전 현재) UI-테스트 안드로이드 에스프레소를 사용하고 무엇 내가하고 싶은 일 : - 이제 에스프레소가 전화와 태블릿 테스트를 구별하기를 원합니다. - 테스트 A는 태블릿에서만 실행해야하며 테스트 B는 전화와 테스트 C에서만 실행해야합니다. - 테스트가 있어야합니다. 그라데이션 작업을 통해 실행 가능

+0

Android Studio를 사용하고 있습니까? – Xcihnegn

+1

'@ Phone'과'@ Tablet','@DeviceConfig (smallestWidth = 480)'과 같은 주석이 있다면 정말 멋지 겠지요. 그래서 여러분은 주석을 가지고 테스트 메소드에 주석을 달 수 있습니다. , 특정 테스트 방법을 실행할지 여부를 지정합니다. 이것은 아직 존재하지 않지만 가지고있는 것이 좋을 것입니다 ... 그냥 말하기 ... –

답변

16

세 가지 옵션 모두 gradlew connectedAndroidTest 또는 cust를 통해 실행 가능합니다. 톰 Gradle을 작업 : Assumptions with assume - junit-team/junit Wiki - Github에서 org.junit.Assume

1. : 무시로 실패 가정에

기본 JUnit을 주자 취급 테스트합니다. 사용자 정의 러너는 다르게 동작 할 수 있습니다.

불행히도, android.support.test.runner.AndroidJUnit4 (com.android.support.test:runner:0.2) 러너는 실패한 가정을 실패한 테스트로 간주합니다. 이 문제가 해결되면

은, 다음 (isScreenSw600dp() 소스에 대해 아래의 옵션 3 참조) 작동합니다 :

전화 전용 : 모든 시험 방법을 수업 시간에

@Before 
    public void setUp() throws Exception { 
     assumeTrue(!isScreenSw600dp()); 
     // other setup 
    } 

구체적인 시험 방법

@Test 
    public void testA() { 
     assumeTrue(!isScreenSw600dp()); 
     // test for phone only 
    } 

    @Test 
    public void testB() { 
     assumeTrue(isScreenSw600dp()); 
     // test for tablet only 
    } 

2. 사용자 정의의 JUnit 규칙 A JUnit Rule to Conditionally Ignore Tests에서

은 :

이것은 ConditionalIgnore 주석과의 JUnit 런타임으로 후크하는 해당 규칙을 만드는 우리를 이끌었다. 문제는 간단하고 최고의 예를 들어 설명입니다 :

public class SomeTest { 
    @Rule 
    public ConditionalIgnoreRule rule = new ConditionalIgnoreRule(); 

    @Test 
    @ConditionalIgnore(condition = NotRunningOnWindows.class) 
    public void testFocus() { 
    // ... 
    } 
} 

public class NotRunningOnWindows implements IgnoreCondition { 
    public boolean isSatisfied() { 
    return !System.getProperty("os.name").startsWith("Windows"); 
    } 
} 

ConditionalIgnoreRule 코드 여기 : JUnit rule to conditionally ignore test cases.

이 방법은 아래의 옵션 3에서 isScreenSw600dp() 메서드를 구현하도록 쉽게 수정할 수 있습니다. 시험 방법

이 완전히 통과로 시험이보고됩니다 생략 특히 때문에, 최소한의 고급 옵션이지만, 구현하기가 매우 쉽다에서


3. 조건문. 다음은 시작할 수있는 전체 샘플 테스트 클래스입니다.

import android.support.test.InstrumentationRegistry; 
import android.support.test.runner.AndroidJUnit4; 
import android.test.ActivityInstrumentationTestCase2; 
import android.util.DisplayMetrics; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.assertion.ViewAssertions.matches; 
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 

@RunWith(AndroidJUnit4.class) 
public class DeleteMeTest extends ActivityInstrumentationTestCase2<MainActivity> { 
    private MainActivity mActivity; 
    private boolean mIsScreenSw600dp; 

    public DeleteMeTest() { 
     super(MainActivity.class); 
    } 

    @Before 
    public void setUp() throws Exception { 
     injectInstrumentation(InstrumentationRegistry.getInstrumentation()); 
     setActivityInitialTouchMode(false); 
     mActivity = this.getActivity(); 
     mIsScreenSw600dp = isScreenSw600dp(); 
    } 

    @After 
    public void tearDown() throws Exception { 
     mActivity.finish(); 
    } 

    @Test 
    public void testPreconditions() { 
     onView(withId(R.id.your_view_here)) 
       .check(matches(isDisplayed())); 
    } 

    @Test 
    public void testA() { 
     if (!mIsScreenSw600dp) { 
      // test for phone only 
     } 
    } 

    @Test 
    public void testB() { 
     if (mIsScreenSw600dp) { 
      // test for tablet only 
     } 
    } 

    @Test 
    public void testC() { 
     if (mIsScreenSw600dp) { 
      // test for tablet only 
     } else { 
      // test for phone only 
     } 

     // test for both phone and tablet 
    } 

    private boolean isScreenSw600dp() { 
     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     mActivity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     float widthDp = displayMetrics.widthPixels/displayMetrics.density; 
     float heightDp = displayMetrics.heightPixels/displayMetrics.density; 
     float screenSw = Math.min(widthDp, heightDp); 
     return screenSw >= 600; 
    } 
} 

+0

'com.android.support.test : runner : 0.5' 가정은 여전히 ​​실패한 테스트로 취급됩니다. – JJD

+0

저는 러너 0.5와 에스프레소 2.2.2를 돌리고 있으며, 적어도 안드로이드 스튜디오 러너에서는 실패한 가정이 무시 된 것으로 표시되어 있습니다. –

관련 문제