如何在特定情况下运行特定的JUnit测试集? [英] How can I run particular sets of JUnit tests under specific circumstances?

查看:101
本文介绍了如何在特定情况下运行特定的JUnit测试集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个专门在Eclipse中开发的Java代码库。
是一组JUnit4测试,可根据预期运行时间分为两个互斥的
子集:


  1. 当开发人员右键单击测试
    类(或包含项目)并选择Run As> JUnit Test时,应运行标准测试。这里没有
    不寻常 - 这正是JUnit在Eclipse中的工作原理。

  2. 运行时测试只应在应用程序启动时从
    以编程方式调用时运行在特定的状态。

这两种类型的测试可能在同一个Java
包中彼此相邻。 (理想情况下,我们可以将它们混合在同一个类中,但
并不是一个硬性要求。)



我的第一个解决方案是注释运行时测试类使用新的
@TestOnLaunch 注释。应用程序能够找到这些类,
并使用
@Test 注释) > JUnitCore.run(类<?> ...)。但是,这些测试会泄漏到上面的
标准场景中,因为Eclipse测试运行器将运行使用 @Test 注释的任何
方法,而不管我的自定义类
注释的意图。



接下来我尝试将 @TestOnLaunch 注释移动到方法水平。
这可以防止运行时测试泄漏到标准
场景中,但现在我似乎无法获得 JUnitCore 运行那些测试
的方法。 运行。(请求)请求为目标,正确的类和
方法,例如,失败使用没有可运行的方法,可能是
,因为它无法找到 @Test 注释(因为它不存在)。



我很想知道是否有JUnit方式解决这个
类问题。大概我可以编写自己的 Runner (运行方法
@TestOnLaunch 注释) - 这是正确的做法?如果是这样,我怎么做
然后用一堆类开始编程测试,
类似于调用 JUnitCore.run(Class<?> ...)

解决方案

如果你不在同一个测试类中混用两种类型的测试方法,下面这个可以提供帮助:



http://johanneslink.net /projects/cpsuite.jsp



您可以使用过滤器功能设置两个测试套件。



<我通过定义几个标记接口在我的项目中设置了三个测试套件:



UnitTests,IntegrationTests,DeploySmokeTests,AcceptanceTests



三个测试套件:

  @RunWith(ClasspathSuite.class)
@SuiteTypes({UnitTests.class ,IntegrationTests.class})
public class CommitTestSuite {}

@RunWith(ClasspathSuite.class)
@SuiteTypes({DeploySmokeTests.class})
public class DeploySmoke TestSuite {}

@RunWith(ClasspathSuite.class)
@SuiteTypes({AcceptanceTests.class})
public class AcceptanceTestSuite {}

现在,您可以通过运行特定的测试套件来实现目标。另一种解决方案是使用junit类别:

  @Category(IntegrationTests.class)
公共类SomeTest {

@Test
public void test1(){
...
}

@Test
public void test2(){
....
}
}

@RunWith(Categories.class)
@IncludeCategory(UnitTests.class,IntegrationTests.class)
@SuiteClasses({//所有测试类})
公共类CommitTestSuite {}

正如我所说,如果你在一个测试类中混合使用不同类型的测试方法,第一个测试类无法帮助你,但是通过使用seconde解决方案你可以在测试方法上注释你的类别接口(我在上面的例子中在测试类上注释了它) )。但是,如果您选择第二个解决方案,则每次添加新测试类时都必须维护测试套件。


I have a Java codebase that is developed exclusively in Eclipse. There are a set of JUnit4 tests that can be divided into two mutually exclusive subsets based on when they are expected to run:

  1. "Standard" tests should run when a developer right-clicks the test class (or containing project) and selects Run As > JUnit Test. Nothing unusual here—this is exactly how JUnit works in Eclipse.
  2. "Runtime" tests should only run when called programmatically from within the application when it is started up in a specific state.

The two types of tests might sit adjacent to each other in the same Java package. (Ideally we could intermix them in the same class, though that's not a hard requirement.)

My first solution was to annotate the "Runtime" test classes with a new @TestOnLaunch annotation. The application is able to find these classes, and was running the tests contained in them (annotated with @Test) using JUnitCore.run(Class<?>...). However, these tests leak into the "Standard" scenario above, because the Eclipse test runner will run any method annotated with @Test, regardless of the intent of my custom class annotation.

Next I tried moving the @TestOnLaunch annotation to the method level. This prevents the "leakage" of the "Runtime" tests into the "Standard" scenario, but now I can't seem to get JUnitCore to run those test methods. run.(Request) with a Request targeted at the correct class and method, for example, fails with "No runnable methods", presumably because it can't find the @Test annotation (because it's not there).

I'm very interested to know if there's a "JUnit way" of solving this kind of problem. Presumably I could write my own Runner (to run methods annotated with @TestOnLaunch)—is this the right approach? If so, how do I then kick off the testing programmatically with a bunch of classes, analogous to calling JUnitCore.run(Class<?>...)?

解决方案

If you don't mix the two type test method in the same test class, this below may help:

http://johanneslink.net/projects/cpsuite.jsp

You can use the filter feature to setup two test suite.

I setup three test suites in my project by defining several mark interfaces:

UnitTests, IntegrationTests, DeploySmokeTests, AcceptanceTests

And three test suites:

@RunWith(ClasspathSuite.class)
@SuiteTypes({UnitTests.class, IntegrationTests.class})
public class CommitTestSuite {}

@RunWith(ClasspathSuite.class)
@SuiteTypes({DeploySmokeTests.class})
public class DeploySmokeTestSuite {}

@RunWith(ClasspathSuite.class)
@SuiteTypes({AcceptanceTests.class})
public class AcceptanceTestSuite {}

Now you could achieve your goal by running specific test suite. An alternative solution is using junit category:

@Category(IntegrationTests.class)
public class SomeTest {

    @Test
    public void test1() {
        ...
    }

    @Test
    public void test2() {
        ....
    }
}

@RunWith(Categories.class)
@IncludeCategory(UnitTests.class, IntegrationTests.class)
@SuiteClasses( { //all test classes })
public class CommitTestSuite {}

As I said if you mix differenct type test method in one test class, the first one can't help you, but by using the seconde solution you could annotate your category interface on test method (I annotated it on test class in the example above). But if you choose the second solution, you have to maintain your test suite every time you add a new test class.

这篇关于如何在特定情况下运行特定的JUnit测试集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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