I am trying to exclude a single test method from a test file in my gradle project. I have excluded the entire test file as below which works fine.
./gradlew -PexcludeTests="com/in/Mytest*"
The above works and excludes all the test methods in the test file. However, I am looking for a command-line way where I can exclude just one test method.
Related
I have been trying to run all JUnit tests in a directory with Bazel. As far as I know, the java_test rule is only capable of running a specific class. However, I'm looking for behavior more like mvn test which runs all JUnit tests in the project. How can I accomplish this?
The typical way to organize this is to have a java_test rule per Java test class, or per group of related Java test classes. Then java_tests can be grouped together using test_suite, if that's something you want to do.
You can run all tests in a package with:
bazel test //some/package:all
or in a package and its subpackages:
bazel test //some/package/...
or in the entire workspace:
bazel test //...
More about target patterns: https://docs.bazel.build/versions/master/guide.html#target-patterns
If you just want a java_test that runs all the tests in a directory, you can do something like
java_test(
name = "tests",
srcs = glob(["*Test.java"]),
deps = [ ..... ],
)
but that may or may not be the right thing to do. In particular, if you want to just run one test or one test method (e.g. using --test_filter), bazel will still build all the java_test's dependencies. Also, note that globs only glob within a build package, and won't cross over into other packages.
I have a multi-project gradle setup
RootProject
|
---- ProjectA
|
---- ProjectB
ProjectA contains around 500 unit tests. Out of which 3 are failing in a single class MyClass.
Everytime I do:
./gradlew :ProjectA:test
it runs all 500 unit tests.
I am trying to run a single failing test inside MyClass or atleast only tests contained in MyClass to reduce the build time.
I tried:
./gradlew -D:MyProject:test.single=MyClass --no-rebuild :MyProject:test
However, it still continues to run all the tests.
Trying this works
./gradlew :MyProject:test --tests '*MyClass*'
but still takes lots of time as it seems to be parsing names of every test class to find the matching name.
Trying
./gradlew :MyProject:test --tests "com.mypackage1.mypackage2.MyClass.testMethod1"
throws the following error:
> No tests found for given includes: [**/*SomeTest.class](exclude rules) [com.mypackage1.mypackage2.MyClass.testMethod1](--tests filter)
What am I missing? What am I doing wrong?
This seems to be a simple use case which should be very obvious to do.
Edit 1:
RootProject build.gradle has:
configure(subprojects.findAll {it.name != 'SomeProjToIgnore'}) {
test {
forkEvery 1
//Exclude tests
exclude '**/*SomeTest.class'
}
}
Edit 2:
./gradlew :MyProject:test --tests 'MyClass*'
is considerably faster. (notice removed '*' from the front). But still I don't know how to run a single test inside MyClass.
I'm using gradle to build my android project and am not able to run single local unit test. I have several test classes and one of them is MockServerTest and I only want to run test methods in this class.
I tried using gradle -Dtest.single=MockServerTest test but it turned out running all my tests, including these in other test classes.
I also tried gradle test --tests MockServerTest but an error occurred said
Test filtering is not supported for given version of junit. Please upgrade junit version to at least 4.6.
But I'm using junit 4.12 in my gradle file
testCompile 'junit:junit:4.12'
I'm using gradle 2.4 with com.android.tools.build:gradle:1.2.3.
Also, how can I run a single test method inside a single test class?
BTW, I'm able to run single test method inside Android Studio, by right clicking on the test method and select run targetTestMethod() from the menu. But how can I achieve this in the terminal? I guess Android Studio also trigger a certain command to do this. How can I see what that command is?
Figured it out myself. I have to run
gradle testDebug --tests com.my.package.TestClassName
There are two things to note here.
1. You have to use gradle testDebug or gradle testRelease instead of just gradle test. If you have build variant, you have to use gradle testVariantNameDebug or gradle testVariantNameRelease
2. You have to specify the whole qualified class name, means including the package name.
You can use Android Gradle plugin DSL to set up test tasks filters like this:
android {
testOptions {
unitTests.all {
it.testNameIncludePattern = "*.SomeTest"
}
}
}
You can find more information on testOptions here and filters here.
Have you tried running gradle test -Dtest.single=MockServerTest? More information can be found here.
Our project has a suite of Selenium tests that we are currently packaging into a jar with the intent of Jenkins running the tests as a build step. We install the jar into the local repository of a client VM that is configured to have Selenium point back to Jenkins as the host. The trouble we are running into is figuring out a way to get Maven/SureFire to find the Selenium/TestNG tests in the jar we installed. We have a pom with all the dependencies that the tests require on the client, including the jar of tests itself, but when we we run "mvn test" no tests are found. Clearly we are missing something here, any ideas? Thanks.
surefire by default look for file names like Test*.java , *Test.java , *TestCase.java and executes them. if your test doesnt follow any of these pattern then you have to include them explicitly. refer here
Create a main method calling testNG main method and pass your main methods args[] to testNG Main method. Now your test jar will have main method of its to trigger test cases of your desire. You can pass the same paramteres as TestNG to your jar file like testng.xml file or -testClass classapath etc... see example below.
public static void main(String args[])
{
org.testng.TestNG.main(arg);
}
now you just need to create a xml file of test classes and in jenkins use windows batch command to call your jar file on remote machine with required testNG parameters.
I know that it's possible to run a specific test class with -Dtest=MyTest. But is it possible to run a specific test within that class?
I.e. if MyTest defines testFoo() and testBar(), is there a way to specify that only testfoo() should be run?
I'm aware that it's trivially easy to do this in an IDE, but I occasionally need to run tests on the command line on another server.
From Running a Single Test Using Maven Surefire Plugin
With version 2.7.3, you can run only n tests in a single Test Class.
NOTE : it's supported for junit 4.x and TestNG.
You must use the following syntax
mvn -Dtest=TestCircle#mytest test
You can use patterns too
mvn -Dtest=TestCircle#test* test
It will be available as of Surefire 2.8, see SUREFIRE-577
Don't think its available. You can work around it by passing some system properties & ignore execution of tests based on the property value. However it does not seem to add a great value add. There is also TestNG which offers additional features.
http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html
To execute one Test at a time, run mvn test
mvn -Dtest=MyUnitlTest test
To execute one Test at a time and a specific method from it:
mvn -Dtest=MyUnitTest#method test
where MyUnitTest is the name of your test and #method is the name of your method.
Execute tests with surefire:
mvn surefire:test