简体   繁体   中英

Android Simple Unit Test Succeeds when Clearly Wrong

Whenever I run a simple JUnit test on Android studio, the tests always succeed no matter what..

I already hit Build Variant and this is under Unit Tests.

public class ECUserTests extends InstrumentationTestCase{

    public void test() throws Exception{
        final int expected = 1;
        final int reality = 5;
        assertEquals(expected, reality);
    }

I added

testOptions {
        unitTests.returnDefaultValues = true
    }

To my build.gradle file and now this is something that clearly isn't true is returning as passed.

For the new unit test support, you don't need to inherit from InstrumentationTestCase , but you do need to annotate your test method.

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ECUserTests {

  @Test
  public void test() {
    final int expected = 1;
    final int reality = 5;
    assertEquals(expected, reality);
  }

}

To run unit test, it's required to select "Unit test" in Test Artifact under Build Variants and Android Studio version must be >1.1

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM