简体   繁体   English

为什么JUnit测试在Eclipse中运行两次?

[英]Why are JUnit tests run twice from within Eclipse?

A similar question has already been asked here . 这里已经提出类似的问题。

One (unaccepted) answer states: 一个(不可接受的) 答案指出:

the test class will always be started directly and then through the "link" in the suite. 测试类将始终直接启动,然后通过套件中的“链接”启动。 This is as expected. 这是预期的。

Can someone explain what this actually means and whether or not it is possible to prevent the tests running twice. 有人可以解释一下这实际上意味着什么以及是否有可能阻止测试运行两次。

When I run the tests from the command line using mvn test they only run once. 当我使用mvn test从命令行运行mvn test它们仅运行一次。

UPDATE UPDATE

I have a test suite defined as follows: 我有一个定义如下的测试套件:

@RunWith(Suite.class)
@SuiteClasses({ TestCase1.class, TestCase2.class })
public class MyTestSuite
{
}

When you run tests in Eclipse on project level (or package level), Eclipse searches all project's source folders for JUnit classes (or selected package). 在项目级别(或程序包级别)上的Eclipse中运行测试时,Eclipse会在所有项目的源文件夹中搜索JUnit类(或选定的程序包)。 These are all classes with @Test annotations and all classes with @RunWith (probably some more too). 这些都是带有@Test批注的类,以及所有带有@RunWith类(可能还有更多类)。 Then for all these classes it runs them as tests. 然后,对于所有这些类,将它们作为测试运行。

As a result of this behavior, if you have a suite class that references tests classes in the same project, these tests will run twice. 由于这种行为的结果,如果您有一个引用同一项目中的测试类的套件类,则这些测试将运行两次。 If you had another suite that did the same, they would run three times and so on. 如果您有另一个套件执行相同的操作,它们将运行3次,依此类推。 To understand this behavior try running a suite that contains one test case twice, for instance: 若要了解此行为,请尝试运行一个包含两次测试用例的套件,例如:

@RunWith(Suite.class)
@SuiteClasses({ TestCase1.class, TestCase1.class })
public class TestSuite {}

Accepted strategy here is to define a suite or suites for a project an run them exclusively. 此处接受的策略是为项目定义一个或多个套件,然后专门运行它们。 Do not start tests on a project level but run selected suites only. 不要在项目级别上开始测试,而只能运行选定的套件。

As far as Maven is concerned, I suspect that its default configuration only picks out suite class and omits test cases. 就Maven而言,我怀疑其默认配置只会挑选套件类并忽略测试用例。 Had it been configured differently, it would behave the same as Eclipse. 如果对其进行了不同的配置,则其行为将与Eclipse相同。

Elipse tests 2 classes and give you 2 results. Elipse测试2个类,并给您2个结果。 Maven tests 2 classes and give you one result with 2 sub results. Maven测试2个类,并给您一个结果和2个子结果。

I think is somethink like this, but still most important thing is that result are positive! 我认为是这样,但最重要的是结果是积极的! :) Regards! :) 问候!

Same as this question https://github.com/spring-projects/spring-boot/issues/13750 与此问题相同https://github.com/spring-projects/spring-boot/issues/13750

Just exclude individual test cases and include the suite test cases. 仅排除单个测试用例,并包括套件测试用例。

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

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