按预期顺序运行相关和独立的测试方法 [英] Running dependent and independent test methods in expected sequence

查看:23
本文介绍了按预期顺序运行相关和独立的测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按顺序执行测试方法.我在 testng.xml 中使用了类,并将保留顺序设置为 true.

I want to execute test methods in sequence. I have used classes in testng.xml with preserve-order set to true.

<test name="group-test" preserve-order="true" >
    <classes>
        <class name="com.dependency.ClassA">
            <methods>
                <include name="create"/>
                <include name="enter"/>
                <include name="delete"/>
            </methods>
        </class>
    </classes>
</test>

我的测试课是

public class ClassA {


@Test()
public void Create() throws Exception
{
    System.out.println("in method create");

}   
@Test(dependsOnMethods= "Create")
public void Enter() throws Exception
{
    System.out.println("in method Enter");

}
@Test()
public void delete() throws Exception
{
    System.out.println("in method delete");

}

执行测试后我的输出是
在方法创建中,
在方法删除中,
在方法中输入

After executing the test my output is
in method create,
in method delete,
in method enter

但我想要的是先执行创建"然后输入"然后删除"方法.这里delete是一个独立的测试方法.

我阅读了 谷歌群组问题其中 Cedric Beust 提到您可以使用依赖项在 testng.xml 中显式包含测试方法.我不明白为什么这是强制执行?如果我想以我想要的任何顺序一起执行独立和相关的测试方法怎么办?我观察到首先执行独立方法,然后执行依赖方法.

But what I want is to first execute "create" then "enter" then "delete" method. Here delete is an independent test method.

I read on a google group question where Cedric Beust mentions that you can either use dependency OR explicitly include test methods in testng.xml. I don't understand why is this an enforcement? What if I want to execute independent and dependent test methods together in any sequence I want? I have observed that independent methods get executed first and then the dependent methods.


理想情况下,依赖不应该是为了保持顺序,而是在前一种方法失败时跳过测试.TestNG 的那种强制执行造成了很多麻烦!


Ideally dependency should not be for preserving order but to skip test if the previous method fails. The kind of enforcement TestNG has is causing a lot of trouble!

推荐答案

我正在为 testng 的同样主要(我必须说很明显)不足而苦苦挣扎.到目前为止,我发现的最佳解决方案是使用优先级.EG @Test(priority = 10), then next test @Test(priority = 20) 等等.到目前为止我发现的文档和互联网搜索都说使用@Test(priority = 1),然后@Test(priority =2),但是随后您会遇到明显的未来维护问题,每次在某处中间添加一个测试时,都必须重新编号所有测试......所以这个 10、20 等的解决方案要好得多,因为它至少允许您可以在 test1 和 test2 之间添加 @Test(priority = 11)、12 等.它确实有效,我已经验证过.幸运的是,testng 没有强制执行 1、2、3,否则我们真的会遇到麻烦!哦,顺便说一句,如果你有组和方法依赖(除非需要,否则你不应该在所有测试中使用它!)那么它看起来就像 @Test(priority = 10, groups = "login"), @Test(priority = 20, 组 = "登录") 等.此外,您似乎已经知道,但对于其他人来说可能想知道,请记住是否使用依赖项来设置测试运行顺序,然后如果一个失败,则跳过之后的所有测试 - 这根本不是您想要的.无论如何,希望这有助于让您摆脱困境,至少在出现更好的解决方案之前.祝你好运!

I am strugging with the same major (and I must say obvious) deficiency of testng. The best solution I have found so far is to use priorities. EG @Test(priority = 10), then next test @Test(priority = 20), etc. The documentation and internet searches I found so far have all said to use @Test(priority = 1), then @Test(priority = 2), but then you run into the obvious future maintenance problem of having to renumber all your tests every time you add one in the middle somewhere... So this solution of 10, 20, etc. Is much better as it at least allows you to add an @Test(priority = 11), 12, etc in between test1 and test2. It does work, I've verified. Lucky for us testng does not enforce 1,2,3 or we'd really be in trouble! Oh and BTW, if you have group and method dependancies (which you shouldn't use on all tests unless required!) then it just looks like @Test(priority = 10, groups = "login"), @Test(priority = 20, groups = "login")etc. Also, it seems like you already know, but for others maybe wondering, remember if dependancies are used to set test run ordering then if one fails then all tests after are skipped- which is not at all what you want. Anyway, hope this helps to get you unstuck, at least until a better solution comes along. Good luck!

这篇关于按预期顺序运行相关和独立的测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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