在junit4中最后一次测试后执行全局拆卸方法
我想知道是否有办法在执行测试后执行某些方法。 我正在使用 Eclipse,我希望能够运行部分或全部测试,然后执行一些代码, 这样 junit 总是会执行一次我的拆解,总是在最后一次测试之后。
具体问题是我正在使用 selenium webdriver,并且我想在完成所有测试后执行 webdriver 的 quit 方法。首先,我有静态类管理网络驱动程序参考,现在我转向单例模式。我相信可能有更好的解决方案,但现在我不想使用任何 IoC 容器,因为我是 java 初学者。
到目前为止,我尝试了几种方法,但没有成功。
Suit 方法仅在我执行所有测试时才有效:
@RunWith(Suite.class)
@SuiteClasses({SomeTest.class, OtherTest.class, AnotherTest.class})
public class TestSuit {
@BeforeClass
public static void setUp() {
System.out.println("setting up");
}
@AfterClass
public static void tearDown() {
System.out.println("tearing down");
}
}
但是如果我只想运行一个测试类或来自一个特定命名空间的所有测试怎么办?
仅当我的所有测试都分组在一个班级中时,课后方法才有效。
我相信 NUnit 已经实现了 SetUpFixtureAttribute ,它正在做我想要得到什么,jUnit 中有对应的吗?
I would like to know if there is a way to execute some method after executing my tests.
I am using eclipse and i would like to be able to run some or all of my tests and then execute some code,
so that junit will always execute my tear down once, always after the last test.
The concrete problem is that I am using selenium webdriver and i would like to execute webdriver's quit method after all my tests are done. First I had static class managing webdriver reference, now i moved to singleton pattern. I believe there could be better solution, but right now I don't want to use any IoC containers since I am java beginner.
So far i tried few approaches but none was successful.
Suit approach only works when i execute all my test:
@RunWith(Suite.class)
@SuiteClasses({SomeTest.class, OtherTest.class, AnotherTest.class})
public class TestSuit {
@BeforeClass
public static void setUp() {
System.out.println("setting up");
}
@AfterClass
public static void tearDown() {
System.out.println("tearing down");
}
}
but what if i would like to run just one test class or all tests from one specific namespace?
After class approach works only if all my test are grouped in one class.
I believe that NUnit has implemented SetUpFixtureAttribute , which is doing what i would like to gain, is there any counterpart in jUnit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 TestNG 来代替?您可以使用 @BeforeTest/@AfterTest 或 @BeforeSuite/@AfterSuite 在一组测试或一组测试之前/之后运行浏览器打开/关闭方法。 (http://testng.org/doc/documentation-main.html)
<套房><测试><类>...< /类>< /测试>... < /套房>
就我个人而言,我使用 @BeforeMethod/@AfterMethod 来加载和关闭浏览器,因此我在每次新测试之前都会获得一个干净的浏览器实例。当然,这样做需要更长的时间,但这只是我根据我正在运行的测试/软件的类型做出的选择。这还允许我在并行线程中运行测试,如果我要使用 @BeforeClass/@Afterclass,则不必担心测试类中的测试分组。
Have you considered using TestNG instead? You can use the @BeforeTest/@AfterTest or @BeforeSuite/@AfterSuite to run your browser open/close methods before/after a group of tests or suite of tests. (http://testng.org/doc/documentation-main.html)
< suite>< test>< classes>...< /classes>< /test>... < /suite>
Personally, I use the @BeforeMethod/@AfterMethod to load and close my browser, so I get a clean browser instance before each new tests. It takes longer of course to do this, but it's just a choice that I made based on the kind of tests/software I'm running. This also allows me to run my tests in parallel threads, without having to worry about the grouping of tests in my test classes if I were to use @BeforeClass/@Afterclass.
如果您想在这种情况下使用 JUnit,您可以做的是提供所使用的测试运行器的替代或更改版本。只需在测试用例中设置一个调试标志并调试其后发生的情况即可。迟早您会在正确的位置实现测试运行器。
现在是你的时间了。只需将类复制到您的 src 文件夹中(您可以使用额外的 src 文件夹或在 src/test/java 中,以免弄乱您的代码库)。现在以您喜欢的方式更改类(必须位于原始包中并具有原始类名)。
有趣的部分是类路径是由引导加载程序首先加载的类路径(由 eclipse / 和其他)构建的。这样,在原始 junit 版本有机会加载之前,您提供的版本就会首先被获取。
现在,您可以调用测试运行程序扩展之类的东西(或任何您想调用的东西),并完全控制一切,因为您与原始类位于同一个包中。
我自己只是将其扩展为按照在类文件中声明的方式对方法进行排序(使用 asm 读取类文件以获得方法的正确顺序)。
此外,我还添加了扫描包和子包以进行其他测试的行为,因此我不必使用套件,甚至不必为其提供一些类来构建。现在,通过发现其他测试用例,可以动态构建子套件。我的套房也是如此。有时我需要特定子包中的所有测试,因此我放入一个用 suite 注释的类。
这就是跑步者通过组成套件所做的全部工作。人们甚至可以为游戏添加注释。因此,我对测试进行注释,如 @Fast,并在测试运行程序中仅运行快速测试,因为该套件也注释为 @Fast。你明白了....
What you can do if you want to use JUnit in such a case is providing the alternative or altered version of the test runner that is used. Just set a debug flag inside your test case and debug what is happening after it. Sooner or later you hit the test runner implementation at the right spot.
Now is your time. Just copy the class into your src folder (you can use an additional src folder or inside your src/test/java, to not mess up your code base). Now alter the class (must be in the original package and with the original class name) in every way you like.
The fun part is that the class path is constructed (by eclipse / and others) that your classes are first to load by the bootstrap loader. This way your provided version is just picked up first before the original junit version has a chance to be loaded.
Now you can call something like a test runner extension (or whatever you want to call it) and have complete control over everything since you are in the same package as the original classes.
I for myself just extended it to order the methods the way they are declared within the class file (used asm to read the class file to get the correct ordering of methods).
Also I added behaviour to scan the package and sub packages for additional tests so I dont have to use suite or even give it some classes to build up. The sub suite are now constructed on the fly by discovering additional test cases. That is also true for my suite. Sometimes I need all tests in a particular sub package so I drop in a class annotated with suite.
That's all the rest does the runner by composing the suite. One might even add annotations to the game. So I annotate the tests like @Fast and run only fast tests within my test runner since the suite is also annotated as @Fast. You got it... .