Java 如何使用 testng.xml 从大型 TestNG 套件中执行一项测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1672639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute one test from large TestNG suite using testng.xml?
提问by Sad Developer
I have a TestNG suite with large amount of methods. I execute this suite using wrapper built on top of TestNG runner. All tests in the suite fail except one. What should I write in testng.xml to execute just that one failed test?
我有一个包含大量方法的 TestNG 套件。我使用构建在 TestNG runner 之上的包装器来执行这个套件。除了一个之外,套件中的所有测试都失败了。我应该在 testng.xml 中写什么来执行那个失败的测试?
Obvious solution is to assign unique group names to all of the methods and then specify name in testng.xml. This can work in case of 2-3 methods, but it gets harder as number of tests grow.
明显的解决方案是为所有方法分配唯一的组名,然后在 testng.xml 中指定名称。这可以在 2-3 种方法的情况下工作,但随着测试数量的增加,它变得越来越难。
回答by blissfool
There are several methods to do this.
有几种方法可以做到这一点。
Are you using Eclipse for development? There is an Eclipse plugin for TestNG and I think it would be by far the easiest way for you to run specific tests. The plugin allows you to run suite, group, class or method of available test.
您是否使用 Eclipse 进行开发?TestNG 有一个 Eclipse 插件,我认为它是迄今为止运行特定测试的最简单方法。该插件允许您运行可用测试的套件、组、类或方法。
If not, I believe you can setup an ant task for launching the test(http://testng.org/doc/ant.html) and use attributes like "classfilesetref" to provide a list of test to run. You can specify the test in a separate file so you don't have to update the build.xml every time your run the test.
如果没有,我相信您可以设置一个 ant 任务来启动测试(http://testng.org/doc/ant.html)并使用“classfilesetref”等属性来提供要运行的测试列表。您可以在单独的文件中指定测试,这样您就不必每次运行测试时都更新 build.xml。
For installing testng Plugin.Just Follow the steps: 1-Go to "Help" Menu in Eclipse. 2-Select "Install New Software"". 3-Add"http://beust.com/eclipse."
安装 testng 插件。只需按照以下步骤操作: 1-转到 Eclipse 中的“帮助”菜单。2-选择“安装新软件” 。3-添加“ http://beust.com/eclipse”。
It works in case of the error you specified I think you do not have the plugin installed within the Eclippse IDE
它适用于您指定的错误我认为您没有在 Eclippse IDE 中安装插件
回答by Cedric Beust
After each run, TestNG creates a filed called testng-failed.xml that contains only the tests that failed. Just invoke TestNG again on that file:
每次运行后,TestNG 创建一个名为 testng-failed.xml 的文件,其中只包含失败的测试。只需在该文件上再次调用 TestNG :
java org.testng.TestNG testng.xml java org.testng.TestNG testng-failed.xml
java org.testng.TestNG testng.xml java org.testng.TestNG testng-failed.xml
(replace org.testng.TestNG with your own runner since you seem to use a customized one).
(将 org.testng.TestNG 替换为您自己的运行程序,因为您似乎使用了自定义的运行程序)。
回答by Jeremy Raymond
You could also create your own ITestListener(since you've got your own wrapper anyhow) that keeps track of the failures and then from that generate your own failures suite file that contains only the failed test. TestNG's listener/interceptor hooks are pretty good. At work we've extended TestNG using them several ways:
您还可以创建自己的ITestListener(因为无论如何您都有自己的包装器)来跟踪失败,然后从中生成仅包含失败测试的自己的失败套件文件。TestNG 的侦听器/拦截器钩子非常好。在工作中,我们使用它们以多种方式扩展了 TestNG:
- capture/playback of generated data sets
- result logging to a database
- customized test output (logs)
- meta data such as IDs, descriptions, for data sets provided by a @DataProvider
- runtime checks of environment dependent restrictions on test cases
- 捕获/回放生成的数据集
- 结果记录到数据库
- 自定义测试输出(日志)
- @DataProvider 提供的数据集的元数据,例如 ID、描述
- 对测试用例的环境相关限制的运行时检查
回答by Pierre Gardin
Try this:
尝试这个:
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<exclude name="testMethod" />
</methods>
</class>
</classes>
回答by smaant
Instead of exclude
, you may use include
. It'll exactly what you want. Only this test will be executed.
取而代之的是exclude
,您可以使用include
. 这正是你想要的。只会执行此测试。
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<include name="testMethod" />
</methods>
</class>
</classes>
回答by Anthony F
Select the test method in the program and go to the top menu "Run" -> "Run" , or do CTRL + F11 , this will start the test independently of the XML test suite.
在程序中选择测试方法并转到顶部菜单“运行”->“运行”,或者执行 CTRL + F11 ,这将独立于 XML 测试套件启动测试。