To serve Exampleunittest and Exampleinstrumentedtest in Android Studio?

Asked

Viewed 81 times

2

Hello, I’m learning to develop on Android and would like to know the utility of these files and how Android uses them.

I noticed that some applications sometimes do not have these files.

inserir a descrição da imagem aqui

1 answer

3


They are test classes. Exampleunittest is for unit tests, they are local tests and do not need an emulator or device to run. You can test methods in this class and create new unit test classes. Example:

@Test
public void get_current_user_test() {
    User mUser = new User();
    mUser.setName("joao");
    mUser.setEmail("[email protected]");
    mUser.setUserId("123");
    User mUser_two;
    UserDao userDao = new UserDao();
    mUser_two = userDao.getCurrentUser("joao", "[email protected]", "123");

    assertEquals(mUser.getEmail(), mUser_two.getEmail());
    assertEquals(mUser.getName(), mUser_two.getName());
}

This method above tests whether the user you created is equal to the user who is in a class of your project for example.

Exampleinstrumentedtest is a class for instrumental testing, and once again, you can create your own classes. This type of test requires an emulator or device and in the background it installs and runs your app. It is for UI testing, but can also be used to test context and dependencies.

Example:

Context mMockContext;
SharedPreferences preferences;

@Before
public void setup() {
    mMockContext = InstrumentationRegistry.getTargetContext();
    preferences = mMockContext.getSharedPreferences(Contract.SETTINGS_PREF, 0);
}

@Test
public void test_encode_string() {
    String from = "<p>htmltext</p>";
    String to = "htmltext\n\n";
    from = NewsFeedDao.encodeString(from);

    Assert.assertEquals(from, to);
}

In this above method is tested the Encoding of a text string recovered from a XPTO site news feed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.