I am trying to add some unit tests to my android app. Following is what I tried.
Created a directory for my tests and created a package for test classes inside it.
Created following test class in added package
public class MyFirstTest extends TestCase {
#Override
protected void setUp() throws Exception {
super.setUp();
}
#SmallTest
public void basicTest() {
assertEquals("abc", "abc");
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
Created a new Android Tests build configuration in Android Studio
Specified module in build configuration settings and choose to run test cases of my test class (specified class name MyFirstTest in build configuration settings)
But, when I run my build configuration, it says that no tests were found in MyFirstTest class.
junit.framework.AssertionFailedError: No tests found in my.package.tests.MyFirstTest
What should I do to make basicTest() method to get identified as a test case?
You must name your tests testSomething(), that is starting with test
Related
Android connectedAndroidTest: NO tests found
gradle :aar-lib:connectedAndroidTest
com.android.builder.testing.ConnectedDevice > No tests found.
FAILED No tests found. This usually means that your test classes
are not in the form that your test runner expects (e.g. don't
inherit from TestCase or lack #Test annotations).
Test class:
#RunWith(AndroidJUnit4.class)
public class FooTest {
#Test
public void testFoo() {
....
}
}
Looked at the test APK, it contains the test class.
I am following the steps exactly as mentioned here to create instrumentation unit test cases. This is my Test class in the androidTest->Java->com.mypackage.name package
#RunWith(AndroidJUnit4.class)
#SmallTest
public class Test {
private List<String> list;
#Before
public void initList(){
list = new ArrayList<>();
}
#org.junit.Test
public void searchPlace() {
assert list.size() == 0;
}
}
But when I execute this test case, I get a message saying
Process finished with exit code 1
Class not found: "com.package.base.Test"Empty test suite.
Is there anything that I am doing wrong?
I've seen this error when the test instrumentation runner isn't set correctly. It should be set to android.support.test.runner.AndroidJUnitRunner in the build file. Also double-check the class package name is correct in the test configuration.
This happens if you Cut-Paste the test from Unit to Instrumental.
Simply goto Main menu -> Run -> Debug -> Edit config...
Delete old and broken tests.
And re-run.
I setup Espresso instrumentation framework to run my Android Functional Automation tests.
For every test, I want to login to the app and delete the app after I finish the test.
So, I setup something like below:
public class FirstSampleTest extends BaseTest {
private final BaseTest baseTest;
// private final ElementUtils elementUtils;
public FirstSampleTest() throws InterruptedException {
this.baseTest = new BaseTest();
}
#Before
public void initiate() throws InterruptedException {
//I have setup login method here to login to the app after it installs
}
#Rule
public ActivityTestRule<SplashScreenActivity> splashScreenActivityActivityTestRule = new ActivityTestRule(SplashScreenActivity.class);
#Test
public void testTheHomeScreen() throws InterruptedException {
//Some tests go here.
}
#After
public void teardown() throws InterruptedException {
//I want to uninstall the app or delete it from the emulator once the test is run
}
}
You can add a gradle task in the Android Studio Before launch section in Run -> Edit Configurations.
Click + -> Add a gradle-aware Make -> :app:uninstallAll
note: "app" in :app:uninstallAll depends on your main module name. So it can be :my_module:uninstallAll, or :company:uninstallAll
Uninstalling the app from the Instrumentation tests is not possible. However, once all the tests are run, the app is uninstalled automatically.
Note: The app is not uninstalled only when a single test is run. Please run the whole build using the command
./gradlew connectedAndroidTest
When creating a new project in Android Studio, I notice that it automatically creates an /androidTest directory under /src, where there is "ApplicationTest.java" class with the following code:
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
I'm guessing this is what Google wants us to use, but after searching for hours, I couldn't figure out how to use this class that was generated for me. Google's official doc seems to only list how to run on Eclipse IDE (not Android Studio), and I couldn't find any code that would let me perform a simple test (say like assertEquals(1,2)). Can someone show me how to write a simple test code using the above default template, and steps on how to run it, preferably from the command line?
EDIT:
I was able to write a simple test that is intended to fail.
ApplicationTest.java in /androidTest/java/path/to/package/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
#Override
protected void setUp() throws Exception {
createApplication();
}
#SmallTest
public void testMultiply() {
assertEquals("This should not pass ", 50, 49);
}
}
I am able to run this from Android Studio, but I just cannot figure out how to run that from the command line. Any help?
As Jared already said, this is an example setup for an instrumentation test.
I guess, you've already taken a look on this: Testing Fundamentals
Simpliest way to run these tests in android studio is to right click the class and click run.
It is also possible to add tests in your run/debug configurations.
UPDATE:
To run the instrumentation tests on command line, use ./gradlew assembleAndroidTest. This command will run all tests in your src/androidTest (instrumentation test) folder.
As njzk2 mentioned, there also is a ./gradlew assembleTest command. This command is for running all unit tests (which should be placed in the src/test folder). For more information about unit testing in android take a look on this: Android Unit Testing Support
EXAMPLE:
Here an example for an instrumentaion test in android:
#Override
public void setUp() throws Exception {
super.setUp();
InputStream is = getContext().getAssets().open("test.xml");
XmlParser parser = new XmlParser(getContext());
parser.parse(is);
...
}
#MediumTest
public void testSomething() throws Exception {
// test some data your parser extracted from the xml file
...
}
As you can see, i need the context for creating the input stream, therefor i have to go for an instrumentation test. The #MediumTest signals e.g. i'm accessing files from storage (see Test Annotations).
I saw that I'm not the only one having this problem but I don't find a correct answer.
I have an android project that I want to test. I create a junit test class for each class of my project.
My problem is when I run my test, I have the following error :
java.lang.NoClassDefFoundError: android/content/Context
This is my class test :
public class DevicesBDDTest extends TestCase {
DevicesBDD bdd;
/**
* #throws java.lang.Exception
*/
protected static void setUpBeforeClass() throws Exception {
}
/**
* #throws java.lang.Exception
*/
protected static void tearDownAfterClass() throws Exception {
}
protected void setUp() throws Exception {
super.setUp();
Context ctx = mock(Context.class);
final MaBaseSQLiteInterface mockMaBaseSQLite = mock(MaBaseSQLiteInterface.class);
bdd = new DevicesBDD(ctx){
#Override
public MaBaseSQLiteInterface createMaBaseSQlite(Context context) {
return mockMaBaseSQLite;
}
};
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void test() {
assertEquals(1, 1);
}
}
My class DevicesBDD has needs an object Context, therefore I create a mock (with mockito). I tried with a object MockContext too, but it's doesn't work.
This is my Java Build Path :
mockito-all-1.9.5.jar
Android 2.1
-> android.jar
Android Dependencies
-> annotations.jar
Junit 3
-> junit.jar
Not sure if I had the same problem as you but I am using gradle and for some reason the tests just wouldn't run anymore, with the same error as you had. I tried cleaning and rebuilding but to no avail. After hours of frustration and trying to find an answer I came across the simple solution in a GitHub thread:
I resolved this issue by removing the .gradle folder in my project and rebuilding the project.
(thanks to vpetrov)
You can run ./gradlew clean test in the terminal.
Fixed the issue by following these steps --
1.Open module level build.gradle file, go to dependencies, go to this line --
testImplementation 'junit:junit:4.12'
2.Change the junit version to anything else below it (like 4.10)
testImplementation 'junit:junit:4.10'
3.Sync project
4.The issue fixed at this point in my case
5.Set the junit version back to what it was before (4.12 in my case) if you want
testImplementation 'junit:junit:4.12'
6.Sync project
Changing the junit version and syncing project worked in my case.
Robolectric version 4.4 seems not to support jdk 14. So I could switch to jdk 13 or update robolectric to 4.5-alpha-1