Skip to content

xionxiao/App4Unit

Repository files navigation

Travis CI

App4Unit

JUnit like automatic testing application framework. This framework can help developers to build real test applications and create test cases using JUnit like style. It also contains Appium script for automation test and build test matrix.

Setup

1. Pre-request

2. Compile & Run

Standalone

Clone the repo and import to Android Studio. Connect real device and Run AutoTestApp.

Auto run with multiple devices

  1. Build the project

    ./gradlew app:assembleDebug
  2. Start Appium Server

  3. Get device list with adb

    adb devices
  4. Run autotest script

    python script/autotest.py -d <device serail numbers seperated by comma>

    for more information execute

    python script/autotest.py -h

Usage

How To Add Test Cases

This test framework is very similar to JUnit4. If you familar with JUnit4 unit testing. You can create your own test use that schema.

1. Add a new test class

@Description("My Simple Test")
public class MyTestCase {
    @BeforeClass
    public static void setupClass() {
        // static setup before test class
    }

    @Before
    public void setup() {
        // Setup code
    }

    @Test
    public void myTestFunction1() {
        assertEquals(4, 2 + 2);
    }

    @Test
    public void myTestFunction2() {
        assertNotEquals(4, 2 + 1);
    }

    @After
    public void tearDown() {
        // Clean up
    }

    @AfterClass
    public static void tearDownClass() {
        // static teardown after test class
    }
}

2. Create a TestRunner

TestRunner runner = new TestRunner();

3. Run test/suite

//Run a single test
runner.run(MyTestCase.class)

//Run test suite
TestSuite suite = new TestSuite();
suite.add(MyTestCase.class)
runner.run(suite);

Asserts

This test framework reuses [org.junit.Assert] as it's assertion mechanism. To use assertion in TestCase:

Assertions

import static com.sparktest.autotesteapp.framework.*

Injection

You can use dagger dependency injection in test cases.

  • Provide a Module
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module(
        injects = {
                TestTest.class,
        }
)
public class TestModule {
    Context context;

    public TestModule(Context context) {
        this.context = context;
    }

    @Provides
    @Singleton
    public Context provideContext() {
        return this.context;
    }
}
  • Use Inject in Test Case
public class TestTest {
    @Inject
    Context context;

    @Test
    public void run() {
        assertNotNull(context);
    }
}

Annotations

@Description

Descriptions of TestCase and Test methods. Description will be displayed on TestCaseListView. If no @Description annotation, class name will be used.

@Test

Methods to test. Methods annoted with @Test should be public method.

@BeforeClass

Method annoted with @BeforeClass will be executed before class instantiated. This method should be public static method.

@AfterClass

Method annoted with @AfterClass will be executed after all test method are finished or test case failed. This method should be public static method.

@Before

Method annoted with @Before will be executed before every @Test method execution.

@After

Method annoted with @After will be executed after every @Test method execution.

@Ignore

Method or Class annoted with @Ignore will be ignored.

Async await/resume

ThreadSafe

About

JUnit like automatic testing framework for real application.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •