Android connectedAndroidTest: NO tests found - android

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.

Related

Empty test suite error : Writing Instrumentation test cases for android

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.

Gradle JUnit android run all but #FlakyTest

I have a few tests that have multiple annotations:
#Test
#LargeTest
#FlakyTest
I found a way to run only #LargeTest from here.
./gradlew app:connectedAndroidTest
-Pandroid.testInstrumentationRunnerArguments.size=large
Is there a way to run all Instrumentation Tests except those annotated as #FlakyTest?
android {
defaultConfig {
testInstrumentationRunnerArgument 'notAnnotation', 'android.test.suitebuilder.annotation.FlakyTest'
}
}

Running test that was created by Android Studio

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).

Android Tests does not recognize my test method

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

Unit/Instrumentation tests Android Gradle. Instrumentation tests won't run

I have Tests set up in an Android project in Android Studio. I have a Unit test class with tests and an Instrumentation test class with tests. When I run gradlew connectedAndroidTest the unit tests run fine but the instrumentation tests are not run.
Here is my project structure
root
- app
- src
- androidTest
- java
- packagename
- UnitTest class
- InstrumentationTest class
The unit test class extends TestCase. The instrumentation test class extends ActivityInstrumentationTestCase2
An example test in the instrumentation test class is
public void testJSONReturnsString() {
String json = JSON.getJSonFeed(getActivity().getApplicationContext(), "http://foo.bar");
assertNotNull(json);
}
In my build.gradle I have
testInstrumentationRunner "android.test.InstrumentationTestRunner"
in the defaultConfig section
Does anyone know why the Instrumentation test class wouldn't run?
In the end it was because I didn't have a default constructor in my instrumentation class!

Categories

Resources