TestNG - 无法在套件中运行两次相同的测试
我有一个TestNG测试套件,除了一件事情以外工作正常:我不能运行两次相同的测试。我想要运行两次测试的原因是我正在测试用户登录/注销的Web服务,我想验证登录是否有效,然后注销工作,然后再次登录以便后续需要用户登录的测试可以继续。
I have a TestNG test suite that is working fine except for one thing: I cannot run the same test twice. The reason I want to run a test twice is that I am testing web services that log a user in/out, and I want to verify that the login works, then that the logout work, and then log them in again so that the subsequent tests, which require the user to be logged in, can proceed.
这是我的testng.xml的相关部分:
Here is the relevant section of my testng.xml:
<suite name="WebServiceTestSuite">
<test name="webServiceTest" verbose="10" parallel="none">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
<include name="registrationActivateTest"></include>
<include name="appUserLoginTest"></include>
<include name="appUserLogoutTest"></include>
<include name="appUserLoginTest"></include>
<include name="sessionValidateTest"></include>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
如您所见,appUserLoginTest被调用两次。但是,当我调试它时,我可以清楚地看到它实际上只是在第一次调用时运行。之后执行继续进行,好像第二行调用它不存在。
As you can see, the "appUserLoginTest" is called twice. However, when I debug this I can clearly see that it is only actually run the first time it is called. After that the execution proceeds as if the second line calling it didn't exist.
这实际上是在TestNG中设计的。我看到两个可能的选项:
This is actually by design in TestNG. I see two potential options:
首先,看看在测试中使用DataProvider,以便可以使用不同的参数重复执行它们。
First, look at using a DataProvider with your tests so they can be executed repeatedly with different parameters.
或者,更改XML布局,以便每个方法都是其自己的标记的一部分。在此配置中,XML会更长,但您可以根据需要多次调用测试方法作为套件的一部分。
Or, alter the XML layout so that each method is part of its own tag. In this configuration, the XML will be longer, but you'll be able to invoke a test method as many times as you like as part of a suite.
编辑:现在有更多细节。
More details now.
在回答你的评论时,这是一个示例XML布局,当我遇到同样的问题时,它对我有用。试试这个:
In response to your comment, here's a sample XML layout that worked for me when I was up against this same problem. Try this:
<suite name="WebServiceTestSuite">
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationActivateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLogoutTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionValidateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
我删除了 verbose =10parallel =none来自
< test>
标签的code>,因为适用于我的布局没有这个......您可以添加 verbose =10
到< suite>
标记,以获取您需要的任何特定功能。请放心使用并发布您的结果,我将很乐意为您提供进一步的帮助。如果没有看到你的跑步者配置或者对你的其他设置了解太多,可能还有其他一些事情需要考虑。
I removed the verbose="10" parallel="none"
from the <test>
tags, as the layout that worked for me didn't have this... You may be able to add the verbose="10"
to the <suite>
tag to get any specific functionality there that you need. Have a go with this and post your results and I'll be happy to help further. Without seeing your runner configuration or knowing too much about the rest of your setup, there may be some other things to consider.
你会发现这是一个更多详细的XML布局,但它在当时为我解决了同样的问题。您还可以考虑重新安排代码并制作一个@Test方法,将该XML上的所有方法作为单个测试调用,从而允许您减少XML文件的长度。
You'll find that this is a much more verbose XML layout, but it solved this same problem for me at the time. You could also consider re-arranging your code a bit and making an @Test method that calls all the methods on this XML as a single test, allowing you to reduce the length of your XML file.