I am running some tests on an Android phone using gradle. However I would like to be able to select which tests to run. For the tests I am using jUnit4 and categories.
This is how the tests are build and executed from jenkins:
call gradle assembleDebug assembleDebugAndroidTest
call gradle connectedDebugAndroidTest
This is how a test looks like:
#Category(IncludeTest.class)
#Test
public void test_Test_06() throws Exception {
TestData.setUpTest("test_Test_06");
Log.d("Test: Test 6 included");
}
#Category(ExcludeTest.class)
#Test
public void test_Test_07() throws Exception {
TestData.setUpTest("test_Test_07");
Log.d("Test: Test 7 excluded");
}
In my gradle.build I have tried the following without success:
test {
useJUnit {
includeCategories 'com.abc.def.IncludeTest'
excludeCategories 'com.abc.def.ExcludeTest'
}
}
My structure is as follows:
/someFolder/gradle.build
/someFolder/app/src/android/java/
In java i have a package named com.abc and in that package there is another package, def where my IncludeTest and ExcludeTest interfaces are.
I have tried different paths to Include/ExludeTest in gradle.build but it just does not work, all test all always executed.
I have also tried putting the includeCategories/excludeCategories in a task and made sure the task was actually started. But still all test were executed. Just seems like includeCategories/excludeCategories does not do anything.
Is there anything basic I am doing wrong? Are there any other ways of selecting categories?
After some more research I found out that the includeCategories/excludeCategories does not work with Android.
I found a different solution when using AndroidJUnitRunner. In Gradle it is possible to filter tests on annotations. To include annotations:
call gradle connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.annotation=com.abc.def.IncludeTest
There is also the possibility to exclude annotations using notAnnotation instead.
And of course there is syntax if you want to use multiple annotations with OR/AND combinations.
The following is also possible if you are not using connectedDebugAndroidTest:
adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner
Some documentation: https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html
Related
While executing the script using TestNG, it is skipping the main. Below is a sample code, which is similar to mine. If i remove String[] args, then the code is working with TestNG, but running as java application option is removed. How can i overcome this?
Package tests;
import org.testng.annotations.Test;
#Test
public class SearchText {
public void createzoo(String[] args) {
String[] elems = {"lion", "tiger", "duck"};
System.out.println(elems[0]);
System.out.println(elems[1]);
System.out.println(elems[2]);
}
}
Here is the Answer to your Question:
When you want to execute a Java Project you have 2 ways to do so:
Execute the Java file which contains the main method as:
public static void main(String[] args)
Your Java program will start executing from Line 1 within main method.
If you have integrated TestNG libraries to your Java Project you have got a new approach to execute your Java Project. Once you integrate TestNG there is absolutely no need of writing main method as previous case. In this case you need to execute the Class file which contains the #Test Annotation of TestNG as a TestNG Test. TestNG will execute your methods marked with #Test Annotations one by one as per the logic you specify. Once you execute your Java Project as a TestNG Test a xml file is generated by the name testng-customsuite.xml.
Next you can use your IDE feature Convert to TestNG to convert your Java Project to a TestNG specific Project. On doing that you will observe an xml file by the name testng.xml being created at your Project Level. Now using testng.xml you can also be able to execute the Project as a TestNG Suite
Let me know if this Answers your Question.
If you set #Test on a class, then it means that all methods of the class are test methods, included main.
If a test method has params, then TestNG must know how to inject them.
Currently, there is no way to exclude some method via annotations and an issue related issue already exists: https://github.com/cbeust/testng/issues/1405
As an alternative, you can only annotate methods instead of class and it should work.
This error "XYZ method requires 1 parameters but 0 were supplied in the #Test annotation" is encountered when there is misuse of #Test annotation in the code.
In your case, you can either run the application as java application or as TestNG application. When using TestNG , main is not required and you can use run as TestNG. When running as java application, main method would be required.
If i remove String[] args, then the code is working with TestNG, but running as java application option is removed.
When you remove String[] args, it will not be considered main method required for running java application. It is just any regular method. Hence it is working with TestNG but run as java application option is not applicable.
I'm using Serenity + JBehave in Java, run by Gradle. I have several test runner classes pointing at different sets of stories. I need to be able to specify which one I run. One runner file is below to give an example of my implementation there.
import net.serenitybdd.jbehave.SerenityStories;
public class Debug extends SerenityStories {
public Debug() {
findStoriesIn("**/ldap");
}
}
You can do following in build.gradle, so when ever you run
task runSpecificRunner(type: Test) {
include '**/**YOURRunnerName.class'
}
Then run ./gradlew runSpecificRunner aggregate, you will see only that specific runner will be executed.
I am currently working on a Maven Project, using JUnit for defining tests and Jenkins for CI and am looking into how I can group my tests.
Say I had a test class with 20 tests, but I don't want to run all 20 tests, I want to be able to configure which tests to run. For Example, in another standalone project using TestNG and Selenium you can create a test method with the following annotation:
#Test (groups = { "AllTest" })
public void myTestMethod()
{
.. do something
.. assert something
}
... and then I am able to call which group to run based on an XML configuration.
Is it possible to define such type of groupings using Jenkins? I have researched into this and came across the plugin "Tests Selector Plugin" however can't understand how to get started once I've installed the plugin. There is a Wiki Page for it but I can't understand what to do after installing.
I have copy pasted the example property file, and didn't really understand what I needed to manipulate in it. When building, I simply get that the property file cannot be found or Jenkins doesn't have permission; can't find a way around this either :(
It's possible via maven + maven-surefire-plugin
http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
You can run a single test, set of tests or tests by regexp.
So I edited the name of a JUnit test and now it wont work. Instead I get Unrooted Tests: initializationError.
This is a simple test. Infact it is a test for JUnit tests as I am just starting to use it.
#Test
public void testRun()
String s = null;
assertNull(s);
}
and all i did was change it to testRun2(). Also when I run the file not the individual test, it still runs the old testRun(), not testRun2().
My project has Maven not sure if that is a factor. And I have updated the project
So it turned out that I needed to rebuild using Maven to update the classes. Now it works fine and I can add/modify test cases.
In my case, i changed the method name and it didn't update it automatically, so the above solution of Project> Clean worked well for me.
Another way this error would occur is forgetting the Test annotation. Encountered when right click method name in Eclipse and Run As -> Junit Test.
I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.
I can run the whole set of tests from the class using the following:
java -cp .... org.junit.runner.JUnitCore org.package.classname
What I really want to do is something like this:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
or:
java -cp .... org.junit.runner.JUnitCore org.package.classname#method
I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.
So I am wondering if there is any way to do this?
Key points I'm looking for:
Ability to run a single test from a JUnit test class
Command Line (using JUnit)
Avoid modifying the test source
Avoid using additional tools
You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
You can invoke it like this:
> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).
NOTE:
for JUnit version >= 4.9 you need hamcrest library in classpath
I use Maven to build my project, and use SureFire maven plugin to run junit tests.
Provided you have this setup, then you could do:
mvn -Dtest=GreatTestClass#testMethod test
In this example, we just run a test method named "testMethod" within Class "GreatTestClass".
For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
The following command works fine.
mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test
We used IntelliJ, and spent quite a bit of time trying to figure it out too.
Basically, it involves 2 steps:
Step 1: Compile the Test Class
% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java
Step 2: Run the Test
% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest