Execute Cucumber Test scenarios using Gradle - java

I am trying to run Cucumber test scenarios (java project) through Gradle by following reference link-https://docs.cucumber.io/tools/java/#build-tools
This is my Build.gradle file
.output of command gradle build
this is showing scenarios are being recognized but WebDriver is not invoking and reports(build/tests/test/index.html) are showing zero.
I am very new to both gradle and cucumber and stuck in my first gradle project.
help will highly appreciated.

Change the glue parameter in task from "gradle.cucumber" to the classpath where your stepdefinitions are present. Switch to org.projectName.appname.tests and try
gradle.cucumber was the location of the package of stepdefinitions specific to the article.

Note- I got it with second another way is --> when we are using TestNG framework, in the build.gradle file we just need to add the following
test{
useTestNG()
}

Related

How does TestNG look for Test Classes?

How does TestNG work when it is looking for its test classes?
I created a separate project from my TestNG project using JavaFX to create a GUI, when the GUI is run it enables you to select an XML file and then the path to the testNG XML is saved in a variable.
When I run the following code:
String xmlFileName = selectedFile.getAbsolutePath();
TestNG testng = new TestNG();
List<String> testFilesList = new ArrayList<String>();
testFilesList.add(xmlFileName); //test suite resides in the working directory's root folder
testng.setTestSuites(testFilesList); //you can addd multiple suites either here by adding multiple files or include all suites needed in the testng.xml file
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();
I get an error saying that it cannot find my test classes inside the XML.
[TestNG] [ERROR]
Cannot find class in classpath: com.emc.qe.u360.tests.LogoutTests
I then decided to make a java class within my TestNG project, and copy the code over and it still gives the same error message.
Where is TestNG looking for the test classes when the above code is run?
What difference is the code above from manually running the XML from eclipse itself? The code seems to be working, as from what I can see if the code for running the XML was broken or not working, it wouldn't be able to give me that error as it wouldn't be able to determine what is specified in the XML.
Any help would be appreciated, thank you.
EDIT: Just for background information, currently I am using Jenkins to run the tests, what I'm trying to do with the GUI is to create an alternative solution to running the tests outside of the Project/Framework, that can be used universally, a user can just select their XML and then the tests will be executed.
It's configurable if you use the Maven Surefire or the Gradle TestNG runner, but by default I think TestNG looks in src/test/java for your class names. Additionally, I think if your running TestNG from code, as a standalone run, you might be able to adjust the path-base where it looks for classes.

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 simple JUnit4 test in Android Studio 1.1?

I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.
I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor
public class Shape {
public Shape(int i){
if (i==0){
throw new IllegalArgumentException("Cant have 0");
}
}
}
Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?
This is where my question stops. Below I'll go through what I tried.
Please note that I really have never tested before in Android or Java. Excuse me for "rookie" mistakes.
While in the Shape.java I go to "Navigate" > "Test"
Hit enter to select "Create new Test"
Get this popup, and select JUNIT4.
I then hit the fix button to fix the library not being found
I get this popup
I'm not really sure what to select, so I select the default/highlighted.
I write my test
package com.eghdk.getjunit4towork;
import org.junit.Test;
import static org.junit.Assert.*;
public class ShapeTest {
#Test(expected = IllegalArgumentException.class)
public void testShapeWithInvalidArg() {
new Shape(0);
}
}
At this point, I'm not really sure how to run my tests, but try to do this:
I get these errors when running
Error:(3, 17) Gradle: error: package org.junit does not exist
Error:(5, 24) Gradle: error: package org.junit does not exist
Error:(8, 6) Gradle: error: cannot find symbol class Test
Since Android Studio 1.1, there is (experimental) unit test support. A couple of quotes from that page:
You will have to specify your testing dependencies in the build.gradle
file of your android module. For example:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
To use unit testing support in AS, you have to do the following steps:
Update build.gradle to use the android gradle plugin version 1.1.0-rc1 or later (either manually in build.gradle file or in the UI in File > Project Structure)
Add necessary testing dependencies to app/build.gradle (see above).
Enable the unit testing feature in Settings > Gradle > Experimental.
Sync your project.
Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".
Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the
Project view in the Project tool window. The new directory should be
highlighted in green at this point. Note: names of the test source
directories are determined by the gradle plugin based on a convention.
Create your test. You can do this by opening a class, right-clicking its name and selecting "Go to > Test". Add some test
cases.
Right click your new test class or method and select "Run ...".
(Optional) You can decrease the compilation time by using Gradle directly. To do this, go to the Run menu and select "Edit
configurations". There, find the default JUnit template, remove the
"Make" before-launch step and add a "Gradle aware make" step instead
(leave the task name empty).
It is important to know that there are two test types: androidTest and plain test.
androidTest is primarily for tests you run on an emulator or device, such as instrumentation tests. From the command line, you use ./gradlew connectedCheck to run these.
test is for tests you don't want to run on a device, such as the unit test you wrote. You run ./gradlew test to run these tests.
As stated in the quote, you switch between androidTest and test in Android Studio by changing the test artifact.
Naturally, it is preferred to not run tests on a device or emulator, since this speeds up the testing process a lot. With the new experimental unit test support, you gain access to stubbed Android API's without using a device. This lets you move more tests from androidTest to test.
For android studio 1.2 or greater, I include this answer since this is one of the first ranking at google and this is an excelent and VERY easy to follow tutorial on how to set unit tests with Android Studio, this is the link: https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1
After wasting 2 hours trying to run test I finally did it with the above link, hope it is as useful for you as for me.
Nowadays Android Studio (current ver. 1.4) has full Unit test support without any workarounds. Just as suggested in the automatically generated ExampleUnitTest:
To work on unit tests, switch the Test Artifact in the Build Variants view.
Go to settings then build tools then gradle and then experimental. In experimental uncheck enable all test artifacts. Thats it game over

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)

Gradle - how to run the tests from a different gradle project and still get coverage data

Does anyone know how to run the tests from a different gradle project and still get emma coverage reporting data?
Here is my current layout:
Root/
settings.gradle (no explicit build.gradle - just defines all subprojects)
SubProjectA/
build.gradle
src/ (all real source is here)
SubProjectATest/
build.gradle
src/ (all testing code is here)
SubProjectB/ (similar structure as A)
SubProjectBTest/ (similar structure as ATest)
I am currently using the emma plugin, and I would like to build SubProjectA and run all the tests in SubProjectATest from within the build.gradle of SubProjectA.
Here are some things I tried inside the build.gradle of SubProjectA
testCompile project(':SubProjectATest').sourceSets.test.classes (as suggested by this article), but I got an error "Could not find property 'sourceSets' on project"
Just the straight-up testCompile project(':SubProjectATest'), but then I get "..SubProjectA/build/classes/test', not found" and also "Skipping task ':SubProjectA:compileTestJava' as it has no source files."
Simply adding a sourceSet like the following:
test {
java {
srcDir '../SubProjectATest/src'
}
}
Adding the source set in (option 3) is the only option that worked, but it seems sloppy to do it this way. Does anyone know how to do this using project dependencies?
Update #1
I also tried one of the answers below to use test.dependsOn and the tests do run, but the emma plugin reported the following: build/classes/test', not found
1. and 2. just add classes to the test compile class path. This doesn't have any effect on which tests are going to be executed.
3. is the wrong approach because you should not add sources from project X to project Y.
If what you want is that gradle :SubProjectA:test also executes :SubProjectATest:test, all you need to do is to add a task dependency:
SubProjectA/build.gradle:
test.dependsOn(":subProjectATest:test")
By the way, what is your motivation for putting the tests in a separate project?

Categories