Using Gradle task to run Cucumber test with TestNG - java

I want to create Java + Gradle + TestNg + Cucumber project for Automation
I know how to use Gradle tasks to run Cucumber test by specified tags using Junit, but I dont know how to do the same thing with TestNG.
When I use:
task test1(type: Test)
{
systemProperty ("cucumber.filter.tags", "#Test1")
}
in build.gradle it works fine with Junit, but it doesnt work with TestNG.
Thank you for any help in advance.

Related

Integration Tests with Gradle 5.0

I'd like to create dedicated task for integration test in gradle.
I have multimodule project. In project build.gradle I've task:
integrationTest(type:Test){
useJUnit {
includeCategories("examplePackage.IntegrationTest")
}
}
It should run tests marked with:
#Category(IntegrationTest.class)
but when I run this, it says Test events were not received.
What am I doing wrong?
JUnit Platform introduced tagging to replace categories.
Gradle adopted to this. See user guide test grouping

How to run cucumber-gradle automation test script parrallely as well as in sequence manner?

I am writing Web based application automation test script using cucumber,selenium webdriver, Java and gradle.
I have around some 25 feature files.
I need to test this 25 feature files parallel as well as in sequence manner.
Currently, I am running the one test at a time by using test name.
Is there any way to run the test in parallel as well as concurrent manner?
You can do parallel execution if you provide different cucumber runner classes.
Then you can configure the Test tasks in Gradle to run in parralel:
tasks.withType(Test) {
maxParallelForks = 4
}
And you need to define a test Task which executes your cucumber runners (in this example all have a Cucumber in their classname)
task cucumberTests(type: Test){
includes = ['**/*Cucumber*']
}
Maybe this will help you
P.S. Regarding the sequential running: Cucumber doesn't guarante any running order of the different feature files
If you are open to using Maven, here is the example I used. Worked great for me!
https://opencredo.com/running-cucumber-jvm-tests-in-parallel/

How can intelliJ run gradle task as test

I have a gradle project with "unit test" and "integration tests" tasks defined as follows:
test {
include '**/*Test.class'
}
task integrationTest(type: Test) {
include '**/*IT.class'
}
I created a run configuration in IntelliJ to run all unit tests like image shows:
And did the same with the task 'integrationTest':
IntelliJ 'understands' the test task and run it showing graphical results, like in this image:
The same doesn't happen when it runs the 'integrationTest' task. The results are shown in text, like when I run the task by command line.
Answering my own question...
As far as I know you can't make IntelliJ to run tests of a specific task and the Pattern solution doesn't work so well.
So, the only way I found to effectively separate integration tests in IntelliJ was with the use of a JUnit Category.
Create an interface to represent integration tests.
For example:
public interface IntegrationTest {
}
You have to annotate every integration test class with the category annotation and the created interface:
import org.junit.experimental.categories.Category;
import mycompany.mypackage.IntegrationTest;
#Category(IntegrationTest.class)
public class DbfFileProcessorIT {
...
}
Create a build configuration filtering with Category:
Just add idea plugin to gradle works for me
plugins {
idea
}

How to run single Android local unit test using gradle 2.4. Test filtering is not supported

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.

TestNG Ant tasks vs Surefire

I was wondering how different surefire is when executing TestNG than TestNG ant tasks? The reason is that I am seeing consistent difference in behavior when trying to run a TestNG test that extends a JUnit test base (this is a workaround to run JBehave tests in TestNG described here: http://jbehave.org/documentation/faq/). Surefire detects my test as a JUnit test incorrectly (probably because its base is TestCase), while the Ant tasks run perfectly. Can anyone provide an insight into how TestNG handle both cases?
The test looks as follows:
public class YourScenario extends JUnitScenario {
#org.testng.annotations.Test
public void runScenario() throws Throwable {
super.runScenario();
}
}
The short answer is that the ant task is part of the TestNG distribution, so it's part of our tests and I always make sure that it remains up to date with TestNG.
Surefire is developed as part of the Maven project, and as such, it sometimes lags behind (and just like you, I have sometimes encountered bugs when running my tests with Surefire that didn't happen when running from the command line/ant/Eclipse).
I'll bring this question to the Maven team's attention, maybe they'll have more to say.
This looks to be a known bug: http://jira.codehaus.org/browse/SUREFIRE-575.
Have you tried using a TestNG XML suite definition instead of Surefire's automatic test case detection?

Categories

Resources