TestNG with Cucumber

那年仲夏 提交于 2021-02-19 08:43:06

问题


I am running cucumber with testNG. CucumberRunner class extends AbstractTestNGCucumberTests and CucumberRunner class is specified in testNG.xml file.

If I run a simple TestNG class with testNG.xml then output for testNG results gets displayed in console i.e Total tests run, Failures, Skips as shown below:-

Test.java

package com.cucumber.test;
import org.testng.Assert;
public class Test {
@org.testng.annotations.Test
public void test() {
    Assert.assertEquals(true, true);
}
}

testNG.xml:-

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG" verbose="1">
<test name="TestCuke">
<classes>
<class name="com.cucumber.test.Test">
</class>
</classes>
</test>
</suite>

enter image description here

But when I run testNG.xml with CucumberRunner then the output for testNG results are not getting displayed in console.

testNG.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG" verbose="1">
<test name="TestCuke">
<classes>
<class name="com.cucumber.test.CucumberRunner">
</class>
</classes>
</test>
</suite>

CucumberRunner.java

package com.cucumber.test;
import cucumber.api.CucumberOptions;
simport cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(tags= "@smokeTest",features="src\\newTestFile.feature")
public class CucumberRunner extends AbstractTestNGCucumberTests{
}

enter image description here

How to display testNG results output in console with cucumber?


回答1:


To integrate JUnit cucumber tests with TestNG we need to Follow 3 Steps

STEP 1 : In TestNG xml add path and name of CucumberRunner / TestRunner file in class tag.

This allows TestNG to locate cucumber's TestRunner File

STEP 2: In CucumberRunner / TestRunner file extends AbstractTestNGCucumberTests

This allows Cucumber Tests to be run on TestNG instead of JUnit

STEP 3 In Maven add cucumber-testNG dependency

This provides supporting Jars for cucumber and TestNG integration to work




回答2:


You need to define a parameter in test.java and testng.xml like that:

package com.cucumber.test;
import org.testng.Assert;
public class Test {
@org.testng.annotations.Test("myTest")
public void test() {
    Assert.assertEquals(true, true);
}
}

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG" verbose="1">
<test name="TestCuke">
<parameter name="myName"  value="true"/>
<classes>
<class name="com.cucumber.test.Test">
</class>
</classes>
</test>
</suite>



回答3:


You can get the detailed report in the console, by adding "pretty" as on of the format options under @CucumberOptions.

Eg.,

@CucumberOptions(
        format={"pretty","json:path/to/json_repot.json"},
        features = "Path_to_features_file",
        glue="com.sri.stepDefinition",
        tags={"@smoke,@regression")
        )

public class TestRunner extends AbstractTestNGCucumberTests{}


来源:https://stackoverflow.com/questions/30117560/testng-with-cucumber

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!