Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create test class and folder from android studio?

I want to build unit test from existing android project but the test folder and test class like androidTest and test are deleted. Because this is existing project and I dont know why previous developer deleted them, so I need to create new test folder for this to work.

I try add the libraries to my gradle like this :

// Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'

and create test folder and the test class inside src folder but still not works and I can't run the test.

any idea what the correct steps to achieve this in android studio? since the documentation only showing how to add the test library and the test class but not where to place test folder or create new test folder.

like image 993
Gujarat Santana Avatar asked Aug 11 '16 09:08

Gujarat Santana


People also ask

What is test folder in Android Studio?

The location of your tests depends on the type of test you write. Android projects have default source code directories for local unit tests and instrumented tests. Local unit tests are located at module-name /src/test/java/ . These are tests that run on your machine's local Java Virtual Machine (JVM).

What is a test folder?

A test folder is a plan container which is created under test plans to organize a testing plan. On the Test Folders page, you can use a filter option to narrow down the list of selected criteria displayed in a grid.

When should you use the android test directory?

A typical project in Android Studio contains two directories in which you place tests. The androidTest directory should contain the tests that run on real or virtual devices. Such tests include integration tests, end-to-end tests, and other tests where the JVM alone cannot validate your app's functionality.


1 Answers

You can create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point.

Also make sure to choose the correct build variant.

For more details on how to configure all this and run the tests properly, please refer http://tools.android.com/tech-docs/unit-testing-support

Hope this helps.

like image 74
Neh Avatar answered Sep 28 '22 00:09

Neh