13

Tôi đã tạo hoạt động giả trong thư mục androidTest và đã khai báo hoạt động đó trong tệp AndroidManifest trong thư mục androidTest.Tạo DummyActivity bên trong thư mục androidTest để thử nghiệm

Mục đích cơ bản của tôi là thử nghiệm một đoạn có thể tái sử dụng bằng cách đặt nó vào hoạt động giả với hộp chứa framelayout.

AndroidManifest.xml bên trong thư mục androidTest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.droid.test" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-sdk 
     android:minSdkVersion="18" 
     tools:overrideLibrary="android.support.test.uiautomator.v18" /> 
    <instrumentation 
     android:name="android.test.InstrumentationTestRunner" 
     android:targetPackage="com.droid" /> 

    <application> 
     <uses-library android:name="android.test.runner" /> 
     <activity 
      android:name="com.droid.DummyActivityForTest" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

lớp thử nghiệm của tôi TestWidgets.java

public class TestWidgets extends ActivityInstrumentationTestCase2<DummyActivityForTest> { 
    private AppCompatActivity mActivity; 

    public TestWidgets() { 
     super(DummyActivityForTest.class); 
    } 

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

    @Test 
    public void testAddSpecializationClick() { 
     onView(withId(R.id.widgets_rv)).perform(
       RecyclerViewActions.actionOnItemAtPosition(4, click())); 
     Assert.fail("Not Implemented"); 
    } 

Khi tôi chạy lớp thử nghiệm của tôi ném nó dưới đây có ngoại lệ,

java.lang.RuntimeException: Could not launch activity 
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:373) 
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119) 
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97) 
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104) 
at com.practo.droid.home.TestWidgets.setUp(TestWidgets.java:48) 
at junit.framework.TestCase.runBare(TestCase.java:132) 
at junit.framework.TestResult$1.protect(TestResult.java:115) 
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77) 
at junit.framework.TestResult.run(TestResult.java:118) 
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55) 
at junit.framework.TestCase.run(TestCase.java:124) 
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63) 
at junit.framework.TestSuite.runTest(TestSuite.java:243) 
at junit.framework.TestSuite.run(TestSuite.java:238) 
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103) 
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69) 
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) 
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.practo.droid/.DummyActivityForTest } 
at android.app.Instrumentation.startActivitySync(Instrumentation.java:385) 
at android.support.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:90) 
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:353) 
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:350) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

Tôi không có nhiều kinh nghiệm trong thử nghiệm Android, ai đó vui lòng trợ giúp với một số đề xuất.

Trả lời

7

Có 2 APK được tạo trong khi xây dựng dự án. Đầu tiên là APK có ứng dụng và APK thứ hai chứa thử nghiệm. Nếu bạn đặt hoạt động của mình trong thư mục thử nghiệm, nó sẽ ở trong APK thứ hai được sử dụng để kiểm tra và APK ứng dụng của bạn không chứa nó. Đây là lý do tại sao bạn nhận được lỗi này (vì APK ứng dụng của bạn không có hoạt động như vậy).

Vì vậy, cách duy nhất để bạn có thể đặt hoạt động của mình không có trong thư mục thử nghiệm mà là trong các nguồn. Bạn có thể tạo một số biến thể ứng dụng (xem chi tiết here), vì vậy khi bạn đang tạo APK của mình trong sản xuất, hoạt động giả của bạn sẽ không được bao gồm trong đó.

+0

Cảm ơn @Vyacheslav, hương vị giúp ích ở đây ... Tôi sẽ thử –

1

Tôi đã answered this question here và đặt liên kết đến một dự án đơn giản triển khai nó, vì vậy bạn thậm chí có thể xem mã nguồn hoàn chỉnh cho rằng :-) Tôi sẽ sao chép câu trả lời của tôi ở đây, vì có vẻ như tôi đã đạt được mục tiêu:

Thật dễ dàng! Nói chung, bạn chỉ nên đặt tài nguyên của mình trong thư mục src/androidTest/res. Và đó là! Sau đó, bạn có thể sử dụng nó trong các tệp src/androidTest/java của mình. Có, bạn không thể sử dụng kiểm tra bố cục trong APK sản xuất của mình, nhưng bạn có thể sử dụng bố cục thử nghiệm trong thử nghiệm APK.

Có một số vấn đề có thể gây nhầm lẫn cho bạn. Ví dụ: tự động hoàn thành hoạt động tốt không quá thường xuyên, nhưng, dù sao, nó xây dựng và hoạt động.

Đó là cây thử nghiệm hoàn tất, vì vậy bạn có thể thấy rằng tôi có mà hoạt động bên trong dự án thử nghiệm và không gây ô nhiễm chính một :-) Hãy thoải mái đặt câu hỏi nếu có điều gì vẫn là sai :-)

$ tree androidTest/ 
androidTest/ 
├── AndroidManifest.xml 
├── java 
│   └── ru 
│    └── egslava 
│     └── lib_phone 
│      ├── MainActivityTest.java 
│      ├── TestActivity.java 
│      └── actions 
│       ├── HintViewAction.java 
│       ├── KeepHintViewAction.java 
│       └── SetTextViewAction.java 
└── res 
    ├── layout 
    │   └── activity_main.xml 
    └── values 
     └── styles.xml 

Và, vâng, tôi không thể trả lời câu hỏi cụ thể của bạn vì mọi thứ hoạt động và tôi không thể thấy dự án hoàn chỉnh của bạn :-) Tôi chỉ muốn nói rằng nó hoạt động và bạn chắc chắn không cần phải di chuyển các hoạt động thử nghiệm của bạn đến các dự án không kiểm tra. Vui lòng đặt câu hỏi/đăng mã :-)

Các vấn đề liên quan