assumeTrue()如何在JUnit中工作 [英] How does assumeTrue() work in JUnit
问题描述
我正在寻找一些关于 Assume.assumeTrue()
如何在JUnit中工作的验证。我想在 @BeforeClass
方法中使用它,这样如果不满足条件,我可以避免运行我的测试套件。
但是,我想知道该方法的性质。如果 assumeTrue
收到带有false值的参数,它是否会跳过方法的其余部分(使用 @BeforeClass
进行注释) )或是否执行剩余的其余指令。
我也很好奇它可能对注释的方法有任何其他影响:
@After
@Before
@AfterClass
编辑
在通过一个非常基本的测试运行之后,如果 assumeTrue(false)
一直在运行,那么方法的其余部分将是忽略以及使用 @Test
@After
或 @Before $注释的任何方法C $ C>。
对我来说,有一点令人惊讶的是,方法的其余部分被忽略了,而且它也忽略了 @Before
和 @After
除了 @Test
如果 assumeTrue
放在 @BeforeClass
方法中。文档没有指明那种行为(我在提出问题之前检查过)。
Edit2:使用 @BeforeClass
方法中,assumeTrue()非常合理,因为可能存在环境变量(即您的构建/测试机器对资源有压力) )您可能想在运行测试套件之前检查。这有助于避免因系统速度缓慢而导致测试失败(例如超时)。
所以我仍然不确定为什么这会如此困难...... / p>
我跑过的测试看起来像这样
@BeforeClass
public static void beforeClassMethod()
{
System.out.println(BeforeClass assume);
Assume.assumeTrue(false);
System.out.println(AfterClass假设);
}
@Before
public void beforeMethod()
{
System.out.println(Before);
}
@Test
public void testMethod()
{
System.out.println(Test);
}
@After
public void afterMethod()
{
System.out.println(After);
}
@AfterClass
public static void afterClassMethod()
{
System.out.println(AfterClass);
}
输出:
BeforeClass假设
AfterClass
@Before,@ ByClass
带注释的方法是setUp方法,即你使用的方法配置将由测试方法使用的模拟行为。这不用于运行任何测试用例。与 @After
和 @AfterClass
的情况相同,它应该用于注释拆解方法,不应包含任何测试。
现在,您应该在测试方法中使用Assume.assumeTrue()( @Test
注释) 。是,assumeTrue(),如果使用表达式求值为false调用,则测试将暂停并被忽略。有关详细信息,请查看文档。
PS - 这可能不是答案,但它是为了帮助她朝着正确的方向前进。
编辑 -
对我来说,有点令人惊讶的是,方法的其余部分被跳过了
以及它也忽略了@Before和@After如果将
assumeTrue放在@BeforeClass方法中,则添加@Test。文档没有
指定那种行为(我在询问问题之前检查过)。
要回答这个问题,这一点都不奇怪。 @BeforeClass是设置测试用例时运行的第一种方法。它是一个静态setUp方法,它调用一次来为所有测试用例执行setUp。因此,如果您在此处使用assumeTrue()并返回false,则会抛出assumeViolationException并暂停其他所有将要发生的事情。因此,没有什么对你有用。有道理吗?
I'm looking for some validation as to how Assume.assumeTrue()
works in JUnit. I'd like to use it in a @BeforeClass
method so that I can avoid running my test suite if a condition isn't met.
However, I was wondering as to the nature of the method. If assumeTrue
receives a parameter with a false value, does it skip the rest of the method (that's annotated with @BeforeClass
) or does it execute the rest of the remaining instructions.
I'm also curious of any other implications it might have for methods annotated with:
@After
@Before
@AfterClass
Edit:
After running it through a pretty basic test, if assumeTrue(false)
is ever run, then the rest of the method will be ignored as well as any methods annotated with @Test
@After
or @Before
.
To me it was a little surprising that the rest of the method was skipped as well as the fact that it also ignored @Before
and @After
in addition to @Test
if assumeTrue
is placed in the @BeforeClass
method. The documentation doesn't specify that sort of behavior (which I checked before asking the question).
Edit2: Using assumeTrue()
in a @BeforeClass
method is very plausible, as there may be environmental variables (i.e. your build/test machine is stressed for resources) that you may want to check before running your test suite. This can help avoid getting test failures (like timeouts) caused by a slow system.
So I'm still not sure why this is getting downvoted so hard...
The test I ran it through looked like this
@BeforeClass
public static void beforeClassMethod()
{
System.out.println("BeforeClass assume");
Assume.assumeTrue(false);
System.out.println("AfterClass assume");
}
@Before
public void beforeMethod()
{
System.out.println("Before");
}
@Test
public void testMethod()
{
System.out.println("Test");
}
@After
public void afterMethod()
{
System.out.println("After");
}
@AfterClass
public static void afterClassMethod()
{
System.out.println("AfterClass");
}
Output:
BeforeClass assume
AfterClass
@Before, @BeforeClass
annotated methods are setUp methods i.e methods you use to configure mock behaviour that will be used by your test methods. This is not used for running any test cases. Same is the case with @After
and @AfterClass
, it should be used to annotate tear down methods and should not contain any tests.
Now, you should use Assume.assumeTrue() in your test methods(@Test
annotation). Yes, assumeTrue(), if called with an expression evaluating to false, the test will halt and be ignored. Check docs for more info.
P.S - This might not be the answer, but it is to help her go in the right direction.
EDIT -
To me it was a little surprising that the rest of the method was skipped as
well as the fact that it also ignored @Before and @After in addition to @Test if
assumeTrue is placed in the @BeforeClass method. The documentation doesn't
specify that sort of behavior (which I checked before asking the question).
To answer this question, its not at all surprising. @BeforeClass is the first method to run while setting up your test case. Its a static setUp method thats called once to perform setUp for all your test cases. So if you use assumeTrue() here and it returns false, it throws an assumptionViolationException and halts everything else that's gonna happen. Thus nothing works for you. Makes sense?
这篇关于assumeTrue()如何在JUnit中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!