Selenium junit 测试 - 如何按顺序在测试中运行测试?
我正在使用 junit 和 eclipse 来编写功能测试。
运行单独的测试时,它按照我在类中设置的顺序运行。
例如。
testCreateUser
testJoinUserToRoom
testVerify
testDeleteUser
但是,当我作为套件的一部分运行此测试时(因此在包中),顺序是随机的。
例如,它将进行验证,然后删除用户,然后加入用户到房间,然后创建用户。
我在套件中的测试并不相互依赖。然而,测试中的每个单独测试都依赖于它们以正确的顺序运行。
我有什么办法可以实现这个目标吗?
谢谢。
I am using junit with eclipse to write function tests.
When running an individual test it runs in the order that I have them set within the class.
Eg.
testCreateUser
testJoinUserToRoom
testVerify
testDeleteUser
However when I run this test as part of a suite, (so in a package) the order is random.
It will for example do the verify, then delete user then joinuserToRoom then Createuser.
My tests within the suite are not dependent on each other. However each individual test within a test is dependent on them being run in the correct order.
Is there any way I can achieve this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法保证 JUnit 中测试方法的执行顺序。
套件中测试类的执行顺序得到保证(如果您使用Suite),但是如果通过反射找到测试类,则执行顺序不是(例如,如果您'重新运行一个包Eclipse,或者来自maven或ant的一组测试)。这可能可以由 ant 或 maven 定义,但不能由 JUnit 定义。
一般来说,JUnit 按照源文件中定义的顺序执行测试方法,但并非所有 JVM 都能保证这一点(特别是 JVM 7)。如果某些方法是从抽象基测试类继承的,那么这也可能不成立。 (这听起来像你的情况,但我无法从你的描述中看出)。
有关这方面的更多信息,请参阅我对 JUnit4 开始支持测试排序了吗?是故意的吗?。
那么你可以做什么来解决你的问题呢?有两种解决方案。
在你原来的例子中,你实际上只有一个测试(验证),但你有 4 个方法,两个设置(createUser、joinUserToRoom)和一个拆卸(deleteUser)。因此,您的第一个选择是使用 TestRule,特别是 外部资源。
ExternalResource
允许您定义测试之前/之后的行为,类似于@Before/@After
。但是,ExternalResource
的优点是您可以将其排除在测试之外。因此,您可以在外部资源中创建/删除用户:
对我来说,这更简单、更容易理解,并且您可以获得独立的测试,这是一件好事。这就是我要做的,但是您需要稍微重构一下您的测试。您还可以使用 RuleChain 堆叠这些规则。
如果您确实想在测试方法之间引入依赖关系,您的第二个选择是查看 TestNG,您可以在其中定义一个测试与另一个测试的依赖关系。
You can't guarantee the order of execution of test methods in JUnit.
The order of execution of test classes within a suite is guaranteed (if you're using Suite), but the order of execution if the test classes are found by reflection isn't (for instance, if you're running a package in Eclipse, or a set of tests from maven or ant). This may be definable by ant or maven, but it isn't defined by JUnit.
In general, JUnit executes the test methods in the order in which they are defined in the source file, but not all JVMs guarantee this (particulary with JVM 7). If some of the methods are inherited from an abstract base test class, then this may not hold either. (This sounds like your case, but I can't tell from your description).
For more information on this, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?.
So what can you do to fix your problem? There are two solutions.
In your original example, you've actually only got one test (verify), but you've got 4 methods, two setup (createUser, joinUserToRoom) and one teardown (deleteUser). So your first option is to better define your test cases, using a TestRule, in particular ExternalResource.
ExternalResource
allows you to define before/after behaviour for a test, similar to@Before/@After
. However, the advantage ofExternalResource
is that you can factor this out of your test.So, you would create/delete the user in your external resource:
For me, this is simpler and easier to understand, and you get independent tests, which is a good thing. This is what I would do, but you will need to refactor your tests a bit. You can also stack these Rules using RuleChain.
Your second option, if you really want to introduce dependencies between your test methods, is to look at TestNG, in which you can define dependencies from one test to another.
如果它们的顺序“正确”,那么它们不是多个测试,而是您错误地注释为多个独立测试的单个测试。
最佳实践是以经过批准的 junit 样式(设置 - 操作 - 验证)重写它们,并由执行任何所需通用设置的 @Before 或 @BeforeClass 方法支持。
快速的解决方法是使用一个 @Test 注解的方法来按顺序调用其他测试方法。如果您使用 Junit 不是进行严格的单元测试,而是更像场景驱动的系统测试,那么这将成为首选替代方案。它不一定是此类用途的最佳工具,但在某些情况下确实可以很好地工作。
然后,到目前为止你所要做的就是进行一个测试:
如果你感觉自己是有道德的,那么可以通过额外的新测试来补充它,例如
If they have a 'correct' order, then they are not multiple tests, but a single test that you have incorrectly annotated as being multiple independent tests.
Best practise would to rewrite them in approved junit style (setup - act - verify), supported by @Before or @BeforeClass methods that did any required common setup.
Quick workaround would be to have a single @Test-annotated method that called the other test methods in sequence. That becomes something like the preferred alternative if you are using Junit not to do strict unit testing, but something more like scenario-driven systems testing. It's not necessarily the best tool for such use, but it does work perfectly well in some cases.
Then, what you would have so far is have a single test:
which could then, if you are feeling virtuous, be supplemented by extra new tests like