Run single TestNG test with maven by using testng.xml - java

I have defined my tests in a parametrized testng.xml. I use the Failsafe plugin and run my tests with mvn verify. The whole suite is executed.
What if I have to run only one single test from my testng.xml suite? I want that the parameters will be used, but I want to run only one test from the command line.
The maven parameter:
-Dit.test=CheckoutIT#testOrderId
does not work because maven runs the test directly without testng.xml, the parameters are not bind and the test will be ignored.
Is there a way to do it? A workaround is to create a temporary suite xml with only one test but it can't be a solution...
Best regards Robert

According to testng doc:
The command line flags that specify what tests should be run will be ignored if you also specify a testng.xml file, with the exception of -includedgroups and -excludedgroups, which will override all the group inclusions/exclusions found in testng.xml.
Another solution is to add listener to your maven goal which will parse your testng.xml and get test parameters to apply them to your current test.

Related

IntelliJ TestNG customsuite do not copy parameters

I have a issue with the TestNG plugin in IntelliJ. I run my tests by using a testng.xml. The I define parameters on the suite and test level.
If I want to run only one test from IntelliJ by selecting it and run as debug, IntelliJ copies the test into a temporary testng.xml like this one:
/Users/robert/Library/Caches/IntelliJIdea2016.1/temp-testng-customsuite.xml
The problem is, the suite has no parameters defined in my testng.xml file. So TestNG ignores the test because it misses the parameters that doesn't match to the test #Parameters declaration.
How to run a single TestNG test in IntelliJ with using of parametrized testng.xml?
Best regards
Robert
Try this:
On IntelliJ, go to Run > Edit Configurations > Select tab Configuration > Select tab Parameters.
There, add the desired parameters.

How can I find tests that haven't been added to a particular test suite?

How can I find tests that have not been added to a test suite?
If I run the test suite I get say 1000 tests but if I run a file search for "#Test" I get 1020 results. How can I go and add the missing ones without having to go through every search result and comparing to the tests that ran to see which ones are missing?
Use Eclipse itself to create a test suite. From the context menu of the project, select 'New -> Other' and select JUnit test suite. It will list out all the test classes in the project. By default, it creates a AllTests.java source file.

Passing arguments to Junit Runner with (Cucumber.class) from Junit Suite

We need to run a particular Junit Runner class with different tags one after the other . Ex : First #ATest test should run then #BTest test should run .
So the only option we have is create multiple Junit Runner . I was thinking if there would be a way to pass this as an argument from Junit Suite ?
Another approach could be to define the sequence for features in #CucumberOptions.
This wont use the tags, but will run the features from the list in the order mentioned.

Jenkins - Selecting tests

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.

How to run a Cucumber-JVM feature file from the command line

I have a file with file extension .feature. HOw do run this from the command line?
In order to make a batch file for each feature. I am using Cucumber-JVM with Java and Selenium.
Cucumber-JVM is based on JUnit so its just like running any unit tests from the command line
java -cp /path/to/junit.jar org.junit.runner.JUnitCore [test class name]
where test class name is annotated with #CucumberOptions whose features
refer to the feature file. If youre using Maven you could use
mvn test
If you are using Maven, you can run your .feature file this way:
mvn -Dcucumber.options="from/the/root/of/module/to/the/feature/MyFeature.feature" test
This will override the #CucumberOptions in the test runner class.
You can run any test runner or override default test runner by using the command line below. This means you only want to run the test named SmokeTestRunner. By the way, you are free to set surefire plugin in your pom.xml any way. For example, you can set up your surefire plugin in your pom.xml to run the regression test named RegressionTestRunner. It doesn't matter.
mvn -Dtest=**/SmokeTestRunner.java test
Your SmokeTestRunner java file should look like this. ->
#CucumberOptions(
features = "src/test/resources/features/Smoke.feature",
glue = {"stepdefinitions", "hooks"},
tags = "#chrome", // It runs only scenarios tagged with "#chrome"
plugin = {"pretty"/*you can add more cucumber plugins here if you want.*/},
monochrome = true)
public class SmokeTestRunner extends AbstractTestNGCucumberTests {}
For more details, have a look at the maven surefire plugin docs. (https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)

Categories