如何在使用 Junit 4.11 时有条件地执行测试类

huangapple go评论76阅读模式
英文:

How to execute test classes conditionally using Junit 4.11

问题

我有多个测试类,我希望根据一个Bean的值来执行这些测试类。

我的测试类:

@Autowired
protected String abType;

public class abTest extends TestAbstract {

    @Test
    public void testAddUser() {
    ---------
    --------
    --------
    }
}

我希望仅在 abType = a 时执行此类或其测试用例;

TestAbstract 类:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath:application-context-test.xml")
public abstract class TestAbstract {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }
    .
    .
    .
    .
    .
}

所有的测试类都继承了这个类,我想根据在 config.properties 文件中配置的 beanValue 有条件地运行所有的测试类。

我阅读了多篇相关的帖子,但并没有得到我真正需要的内容。
非常感谢任何提供帮助的人!

英文:

I have multiple test classes and I want to execute test classes based on a bean value.

My test class:

@autowired
protected String abType;

public  class abTest extends TestAbstract {

@Test
public void testAddUser() {
---------
--------
--------
}

I want this class or its test cases to execute only when abType = a;

TestAbstract class :

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath:application-context-test.xml")
public abstract class TestAbstract {

 @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }
.
.
.
.
.
}

This class is extended by all the test classes, I want to run all the test classes conditionally based on the beanValue which is configured in config.properties file.

I read multiple posts related to this, but didn't got what I am actually looking for.
Any help would be really appreciated.!!

答案1

得分: 1

我通常在JUnit4中使用assumeTrue。也许这对你也是一个选择。

@Test
public void testOnlyWhenConditionTrue() {
   assumeTrue(conditionTrue);
   ... // 你的测试步骤
}

《使用JUnit 4和5中的假设和条件测试执行》

英文:

I usually use the assumeTrue in JUnit4. Maybe this is an option for you.

@Test
public void testOnlyWhenConditionTrue() {
   assumeTrue(conditionTrue);
   ... // your test steps
}

Assumptions and Conditional Test Execution with JUnit 4 and 5

huangapple
  • 本文由 发表于 2020年7月28日 20:50:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定