简体   繁体   English

assumeTrue()如何在JUnit中工作

[英]How does assumeTrue() work in JUnit

I'm looking for some validation as to how Assume.assumeTrue() works in JUnit. 我正在寻找一些关于Assume.assumeTrue()如何在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. 我想在@BeforeClass方法中使用它,以便在不满足条件时可以避免运行我的测试套件。

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. 如果assumeTrue接收到带有false值的参数,它是否会跳过方法的其余部分(使用@BeforeClass注释)或者是否执行剩余的其余指令。

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 . 在通过一个非常基本的测试运行之后,如果运行了assumeTrue(false) ,那么将忽略该方法的其余部分以及使用@Test @After@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. 对我来说,有一点令人惊讶的是,除了@Test之外,如果将assumeTrue放在@BeforeClass方法中,它还会忽略@Before@After 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 (ie your build/test machine is stressed for resources) that you may want to check before running your test suite. Edit2:@BeforeClass方法中使用assumeTrue()是非常合理的,因为在运行测试套件之前可能需要检查环境变量(即您的构建/测试机器是否有资源)。 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 ie methods you use to configure mock behaviour that will be used by your test methods. @Before, @BeforeClass注释方法是setUp方法,即用于配置测试方法将使用的模拟行为的方法。 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. @After@AfterClass的情况也是如此,它应该用于注释拆解方法,不应该包含任何测试。

Now, you should use Assume.assumeTrue() in your test methods( @Test annotation). 现在,你应该使用Assume.assumeTrue()在您的测试方法( @Test注释)。 Yes, assumeTrue(), if called with an expression evaluating to false, the test will halt and be ignored. 是的,assumeTrue(),如果使用表达式求值为false调用,则测试将暂停并被忽略。 Check docs for more info. 查看文档以获取更多信息。

PS - This might not be the answer, but it is to help her go in the right direction. PS - 这可能不是答案,但它是为了帮助她朝着正确的方向前进。

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. @BeforeClass是设置测试用例时运行的第一种方法。 Its a static setUp method thats called once to perform setUp for all your test cases. 它是一个静态setUp方法,它调用一次来为所有测试用例执行setUp。 So if you use assumeTrue() here and it returns false, it throws an assumptionViolationException and halts everything else that's gonna happen. 因此,如果您在此处使用assumeTrue()并返回false,则会抛出assumeViolationException并暂停其他所有将要发生的事情。 Thus nothing works for you. 因此,没有什么对你有用。 Makes sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM