gpt4 book ai didi

java - 运行来自 IntelliJ IDEA 的 @Category 注解的方法

转载 作者:行者123 更新时间:2023-12-02 11:16:05 25 4
gpt4 key购买 nike

我有一个包含多种测试方法的测试类,我想按某些标准对其进行分组。为此,在方法级别使用 JUnit 的 @Category 注释似乎是一个很好的解决方案:

public class TestClass {
@Test
@Category(AssignmentServiceCategory.class)
public void testMethod1() {}

@Test
@Category(OtherCategory.class)
public void testMethod2() {}
}

我想在 IntelliJ IDEA 中为这些单独的类别创建不同的运行配置,以便仅执行用特定类别注释的测试方法。我的配置如下: IntelliJ IDEA JUnit run configuration

但是,当我运行此命令时,声明该方法的类中的所有测试都会运行,而不是仅运行用指定类别注释的测试。是我的配置不正确,还是 IDEA 只允许类级别的 @Category 注解?

版本:

  • IntelliJ IDEA 2018.1 (181.4203.550)
  • JRE:1.8.0_152-release-1136-b20 amd64
  • JUnit 4.12

最佳答案

已更新

我尝试重现该问题,但未能成功。

这是我的测试类

package com.mytests.category;

import org.junit.Test;
import org.junit.experimental.categories.Category;

public class MyTest {
@Test
@Category(PerformanceTests.class)
public void testMethod1() {
System.out.println("method1");
}

@Test
@Category(RegressionTests.class)
public void testMethod2() {
System.out.println("method2");
}
}

确保您拥有必要的接口(interface)。在 JUnit 中,您需要创建标记接口(interface)来表示类别:

package com.mytests.category;

public interface RegressionTests {}

package com.mytests.category;

public interface PerformanceTests {}

然后在 IntelliJ 中,我运行了一次测试,它会自动为我创建一个配置。然后我编辑配置

Config Screenshot

结果符合预期:仅执行了 testMethod1

旧答案

或者来自 IntelliJ 的文档 ( https://www.jetbrains.com/help/idea/run-debug-configuration-junit.html )

Category Select this option if you only want to run test classes and test methods that are annotated either with the category given with the @IncludeCategory annotation, or a subtype of this category. Fill in the following fields:

Category Specify the desired category. Type category name, or click browseButton and select the desired category in the dialog that opens.

或者您可以创建一个 TestSuite 并指定(在其中)该套件要包含哪些类别。

类似于

package org.mytests.category;

import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Categories.class)
@Categories.IncludeCategory(RegressionTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class RegressionTestSuite {
}

关于java - 运行来自 IntelliJ IDEA 的 @Category 注解的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50278675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com