TestNG:通过testng运行测试时,“未找到测试”

时间:2018-10-04 18:07:07

标签: selenium cucumber testng

我正在尝试通过testng.xml运行简单的Cucumber / Java测试。

所以, 我有testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Cucumber Framework">
<test name="Cucumber Tests" junit="true">
    <classes>
        <class name="CucumberFramework.runner.MainRunner"></class>
    </classes>
</test>
</suite>

我正在使用Runner.class,在其中我将路径/选项/等设置为要素文件,步骤和报告:

package CucumberFramework.runner;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)

@CucumberOptions (
        features = {"src/test/java/CucumberFramework/featureFiles"},
        glue = {"CucumberFramework.steps"},
        monochrome = true,
        tags = {},
        plugin = {"pretty",
                "html:target/cucumber",
                "json:target/cucumber.json",
                "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"}
)

public class MainRunner extends AbstractTestNGCucumberTests {
}

但是当我将Testng.xml作为TestNG套件运行时,它是:

1)自己传递场景,

但是

2)抛出“未找到测试”。 enter image description here

我在做什么错了?

4 个答案:

答案 0 :(得分:1)

具有Cucumber和TestNG的项目不需要使用带有@Test注释的方法的类。

No test were found可能与您运行Cucumber测试的方式有关。

在一个最小的项目下面找到以显示基本需求。

假设以下结构

src/test/java/stepdef/StepDefs.java
src/test/java/runner/RunnerTest.java
src/test/resources/features/test.feature
pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.suboptimal</groupId>
    <artifactId>cuke-testng3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>3.0.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

测试功能

Feature: test
    Scenario: something to test
        Given some step
        Then this is expected

StepDefs.java

package stepdef;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

public class StepDefs {
    @Given("^some step$")
    public void someStep() throws Throwable {
        System.out.println("execute someStep");
    }

    @Then("^this is expected$")
    public void thisIsExpected() throws Throwable {
        System.out.println("execute thisIsExpected");
    }
}

RunnerTest.java

package runner;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = "src/test/resources/features", glue = "stepdef")
public class RunnerTest extends AbstractTestNGCucumberTests {
}

可以通过Maven使用

执行测试
mvn clean test

输出

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running runner.RunnerTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@5eb5c224
execute someStep
execute thisIsExpected

1 Scenarios (1 passed)
2 Steps (2 passed)
0m0.019s

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.552 sec

Test run: 1最后一行所述,黄瓜方案被认为是测试运行。

编辑可以作为TestNG套件执行Cucumber测试。

添加到 pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

创建文件 testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Cucumber Framework" >
    <test name="Cucumber Tests">
        <classes>
            <class name="runner.RunnerTest"></class>
        </classes>
    </test>
</suite>

使用Maven执行测试

mvn clean test

输出

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
execute someStep
execute thisIsExpected

1 Scenarios (1 passed)
2 Steps (2 passed)
0m0.020s

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.614 s - in TestSuite

答案 1 :(得分:1)

@SubOptimal-谢谢您,如此清晰的解释,现在我知道更多了。

关于麻烦本身-终于我删除了testng.xml中提到的junit来解决了问题。正如我在说明中提到的“以testng套件运行”一样,我应该将此答案标记为正确,没有冒犯。

答案 2 :(得分:0)

需要添加@Test注释才能运行。

答案 3 :(得分:0)

将测试数据存储在电子表格中时,我遇到了类似的问题。 我在“执行”为Y的情况下添加了更多列,此问题已解决。