【问题标题】:junit Test with SerenityParameterizedRunner in RunWith annotation: does not find tests (java.lang.Exception: No tests found matching Method)在 RunWith 注释中使用 SerenityParameterizedRunner 进行 junit 测试:未找到测试(java.lang.Exception:未找到匹配方法的测试)
【发布时间】:2019-07-09 23:51:39
【问题描述】:

我正在尝试使用 IntelliJ IDEA 使用 Serenity BDD 框架运行 junit 测试。

当我尝试运行测试时出现错误:

java.lang.Exception: No tests found matching Method ... from org.junit.internal.requests.ClassRequest@71e693fa

这似乎是由于使用 RunWith 注释调用 SerenityParameterizedRunner

@RunWith(SerenityParameterizedRunner.class)

当 RunWith 注释被注释掉时,测试被找到并开始执行(虽然这没什么用,因为我们依赖 Parameterized runner 来构建数据)。

我能够通过一个简单的项目重现该问题以演示该问题。

package com.home;

public class Doorbell {
    private int ringCount = 0;

    public Doorbell() {

    }

    public void ring(){
        System.out.println("Ring!");
        ringCount++;
    }

    public int getRings() {
        return ringCount;
    }
}

测试类:

package com.home;

import net.serenitybdd.junit.runners.SerenityParameterizedRunner;
import net.serenitybdd.junit.runners.SerenityRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(SerenityParameterizedRunner.class)
public class DoorbellTest {

    @Test
    public void testRings()
    {
        Doorbell db = new Doorbell();
        db.ring();
        db.ring();

        Assert.assertEquals(2,db.getRings());
    }
}

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>homeproject</groupId>
    <artifactId>mytestproject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <serenity.version>1.9.31</serenity.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-core -->
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>1.9.31</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-junit -->
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-junit</artifactId>
            <version>1.9.31</version>
        </dependency>
    </dependencies>
</project>

请尝试在项目中运行单个单元测试。非常感谢任何帮助。

【问题讨论】:

  • 你为什么用SerenityParameterizedRunner不带任何参数? AFAIK 这需要 @TestData 以及使用 testdata 作为参数的公共构造函数。
  • 没有将它添加到这个测试类,因为我能够重现这个问题。在实际项目中,我使用@UseTestDataFrom,从csv文件中读取

标签: java junit serenity-bdd


【解决方案1】:

Answer 11 from this Stack Overflow question 解决了我的问题

显然你必须运行包含测试的类,而不是测试本身。

【讨论】:

    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 2022-01-12
    • 2016-03-07
    • 2011-01-20
    • 2020-03-25
    • 2017-04-13
    • 2013-12-02
    • 2015-01-08
    相关资源
    最近更新 更多