Test run failed: Test run failed to complete. Expected 1 tests, received 0

人盡茶涼 提交于 2019-12-01 03:14:07

I had this problem when I didn't have a no-args constructor.

public class MainActivityTest extends
    ActivityInstrumentationTestCase2<MainActivity_> {

public MainActivityTest() {
    super(MainActivity_.class);
}
...

The problem is in your call at

super("nix.android.contact", MainActivity.class);

In my code I have

super("nix.android.contact", Class.forName("nix.android.contact.MainActivity"));

I've also done it this way without have to name the Generic for the ActivityInstrumentationTestCase 2

public class TestApk extends ActivityInstrumentationTestCase2 {

    private static final String TARGET_PACKAGE_ID = "nix.android.contact";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "nix.android.contact.MainActivity";

    private static Class<?> launcherActivityClass;
    static{
            try {
                    launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
            } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
            }
    }

    @SuppressWarnings("unchecked")
    public TestApk() throws ClassNotFoundException {
            super(TARGET_PACKAGE_ID, launcherActivityClass);
    }

I had the same issue while running instrumentation tests on Android (@RunWith(AndroidJUnit4.class)).

I had the following error:

Tests on Nexus_5X_API_26(AVD) - 8.0.0 failed: 
Test run failed to complete. Expected 156 tests, received 152

The problem was that one of the test classes was failing inside a method marked with @BeforeClass, hence no tests were executed for that particular class. Moreover that, the exception which was thrown inside @BeforeClass didn't end-up in the tests-output/report. That's why it was hard to find a reason of "Expected N tests, received M" error message.

So, if you run into the same issue - check your @Before and @BeforeClass implementations - an exception there could be the reason. Hope this helps.

Check that you're not proguarding out a method that your test's contructor depends upon but that nothing in the application uses - logcat will complain about a missing class or method from your application package.

Try uninstalling the target package, to check it's not left over from an alternate build (if, for instance you use Maven alongside Eclipse).

Javier Torón

I had this error and I fixed it removing the parameter from the constructor method, it was created by Eclipse wizard as:

public OptionsActivityTest( String name ) {

I just had to remove the "String name" to make my tests work again.

Richard

I had the same error message. The problem was that my test method name needed to start with 'test'.

Eg: testMethod1() works whilst method1Test() gives the error.

Sandeep Insan

I had the same problem while I was testing my app.
Sometime it works but most of the times the test fails and raised the same error.

Test failed to run to completion.
Reason: 'Test run failed to complete. Expected 1 tests, received 0'.
Check device logcat for details

I checked the test name in all my test class and it is the same in two class, I changed the name of test and it works when I start test again.

It may also error when my device disconnects from my computer.

All tests name should start with prefix "test".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!