How can I run particular sets of JUnit tests under specific circumstances?

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 ``. 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 ``?

以上就是How can I run particular sets of JUnit tests under specific circumstances?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准