从JUnit中的“之前"方法排除单个测试 [英] Exclude individual test from 'before' method in JUnit

查看:104
本文介绍了从JUnit中的“之前"方法排除单个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试类中的所有测试在执行每个测试之前都执行一个"before"方法(用JUnit的@Before注释).

All tests in my test class execute a 'before' method (annotated with JUnit's @Before) before the execution of each test.

我需要一个特殊的测试才能在方法之前执行此操作.

I need a particular test not to execute this before method.

有办法吗?

推荐答案

不幸的是,您必须编写此逻辑. JUnit没有此功能. 通常,您有2种解决方案:

Unfortunately you have to code this logic. JUnit does not have such feature. Generally you have 2 solutions:

  1. 只需将测试用例分为2个测试用例:一个包含需要在运行前"运行的测试,另一个包含不需要的测试.
  2. 实施您自己的测试运行并注释测试以使用它.创建您自己的注释@RequiresBefore,并使用此注释标记需要此注释的测试.测试运行程序将解析注释并决定是否运行之前"方法.
  1. Just separate test case to 2 test cases: one that contains tests that require "before" running and second that contains tests that do not require this.
  2. Implement your own test running and annotate your test to use it. Create your own annotation @RequiresBefore and mark tests that need this with this annotation. The test runner will parse the annotation and decide whether to run "before" method or not.

第二种解决方案更加清晰.第一个比较简单.您可以选择其中之一.

The second solution is clearer. The first is simpler. This is up to you to chose one of them.

这篇关于从JUnit中的“之前"方法排除单个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆