2014-10-07 7 views
17

मेरे सेटअप: - फोन के साथ Android एप्लिकेशन और गोली संस्करण - मैं (केवल फोन संस्करण के लिए अब, buildagent में फोन के साथ) यूआई-टेस्ट मैचों के लिए एंड्रॉयड एस्प्रेसो उपयोग कर रहा हूँफोन और टैबलेट के लिए एंड्रॉयड एस्प्रेसो टेस्ट

क्या मैं करना चाहता हूं: - अब मैं एस्प्रेसो को फोन और टैबलेट के लिए परीक्षणों के बीच अंतर करना चाहता हूं - तो टेस्ट बी केवल टैबलेट द्वारा निष्पादित किया जाना चाहिए, टेस्ट बी को केवल फोन द्वारा निष्पादित किया जाना चाहिए और टेस्ट सी दोनों - टेस्ट होना चाहिए ग्रेबल कार्य के माध्यम से निष्पादन योग्य

+0

क्या यू एंड्रॉइड स्टूडियो का उपयोग कर रहे हैं? – Xcihnegn

+1

कुछ टिप्पणियां थीं, जैसे कि '@ फोन' और' @ टैबलेट ', या' @ डेविस कॉनफिग (सबसे छोटी चौड़ाई = 480) ', ताकि आप इन एनोटेशन के साथ अपने परीक्षण विधियों को एनोटेट कर सकें और अपने डिवाइस प्रकार के आधार पर , एक विशेष परीक्षण विधि निष्पादित या नहीं करने के लिए। यह अभी तक अस्तित्व में नहीं है, लेकिन यह अच्छा होगा ... बस कह रहा है ... –

उत्तर

16

तीन विकल्प, जिनमें से सभी gradlew connectedAndroidTest या ग्राहक के माध्यम से निष्पादन योग्य हैं ओम Gradle कार्यों:

1. उपयोग org.junit.Assume

Assumptions with assume - junit-team/junit Wiki - Github से:

डिफ़ॉल्ट 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 
    } 

,210

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: धावक के साथ: 0.5' धारणाओं को असफल परीक्षण के रूप में माना जाता है। – JJD

+0

मैं रनर 0.5 और एस्प्रेसो 2.2.2 चला रहा हूं और कम से कम एंड्रॉइड स्टूडियो धावक में असफल मान्यताओं को अनदेखा किया गया है। –

संबंधित मुद्दे