[英]Run TestCase from java and display results on Eclipse JUnit view
I defined some classes with, each one, several public methods with @Test annotation. 我定义了一些类,每个类都有几个带有@Test批注的公共方法。 All methods follow the same behavioral pattern (retrieve ressources from IDs, test if empty, log, call the real test for each line on the resource).
所有方法都遵循相同的行为模式(从ID检索资源,测试是否为空,记录日志,为资源上的每一行调用真实测试)。 So, I've externalized this behavior in an abstract class I instanciate on each method, like this:
因此,我在每个方法实例化的抽象类中将此行为外部化,如下所示:
@Test
public void someTest(){
new BasicTestPattern("X","Y","Z"){ // some parameters to retrieve resources
@Override
protected void testLine(){
someCheck1();
someCheck2();
}
}.run();
}
This solution eliminate 10-30 lines per test method. 该解决方案消除了每种测试方法10-30条线。 Now, I want to go further with a custom annotation, like that:
现在,我想进一步介绍自定义注释,如下所示:
@TestPattern(param1="X",param2="Y",param3="Z")
public void someTest(){
someCheck1();
someCheck2();
}
Finally I created a little framework to retrieve all the methods with this new annotation in order to instanciate BasicTestPattern and execute it. 最后,我创建了一个小框架来检索带有此新注释的所有方法,以实例化BasicTestPattern并执行它。 It is executed well in a TestCase subclass, like that:
可以在TestCase子类中很好地执行它,如下所示:
TestCase junit_test = new TestCase(){
@Override
public void runTest() {
pattern.run();
}
};
junit_test.run();
However, no Test is displayed/listed in the JUnit view from Eclipse. 但是,从Eclipse的JUnit视图中没有显示/列出测试。 I see only the number of tests succeeded.
我只看到成功测试的次数。 How can I do that ?
我怎样才能做到这一点 ? Thank you.
谢谢。
You probably will need to make your own custom Runner
to find all the methods annotated with your @TestPattern
method. 您可能需要制作自己的自定义
Runner
来查找所有@TestPattern
方法注释的方法。 (and probably also with @Test
?) (也可能使用
@Test
吗?)
Then your test class will look like this: 然后您的测试类将如下所示:
@RunWith(YourRunner.class)
public class YourTest{
@TestPattern(param1="X",param2="Y",param3="Z")
public void someTest(){
...
}
@Test
public void anotherNormalTest(){
...
}
}
This Blog explains how to write custom Runners. 该博客介绍了如何编写自定义跑步者。 But you can probably get away with extending
BlockJUnit4ClassRunner
to add the your special test methods to the list of tests to run. 但是您可能可以通过扩展
BlockJUnit4ClassRunner
来将特殊的测试方法添加到要运行的测试列表中,从而BlockJUnit4ClassRunner
。
I think you would only have to override the computeTestMethods()
method which is how BlockJUnit4ClassRunner finds all the test methods to run (the methods annotated with @Test) you can override it to find the methods you annotated with your own annotation. 我认为您只需要重写
computeTestMethods()
方法即可,这是BlockJUnit4ClassRunner查找要运行的所有测试方法(用@Test注释的方法)的方法,您可以覆盖它以查找使用自己的注释注释的方法。
public class your TestRunner extends BlockJUnit4ClassRunner{
protected List<FrameworkMethod> computeTestMethods() {
//this is all the @Test annotated methods
List<FrameworkMethod> testAnnotatedMethods = super.computeTestMethods();
//these are all the methods with your @TestPattern annotation
List<FrameworkMethod> yourAnnotatedMethods = getTestClass().getAnnotatedMethods(TestPattern.class);
//do whatever you need to do to generate the test
//methods with the correct parameters based on
//the annotation ?
//Might need to make fake or
//synthetic FrameworkMethod instances?
...
//combine everyting into a single List
List<FrameworkMethod> allTestMethods =...
//finally return all the FrameworkMethods as a single list
return allTestMethods;
}
}
You might have to make your own FrameworkMethod
implementation wrapper to get the info from the annotation and do whatever set up is required before invoking the method. 您可能必须制作自己的
FrameworkMethod
实现包装器,才能从批注中获取信息,并进行调用该方法之前所需的任何设置。
This will make it seamlessly integrate with normal JUnit classes and work with the JUnit IDE view 这将使其与普通的JUnit类无缝集成并与JUnit IDE视图一起使用
Good Luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.