Java 如何使用 Eclipse 创建“testng.xml”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30377239/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:31:27  来源:igfitidea点击:

How to create 'testng.xml' using Eclipse

javaeclipseseleniumtestng

提问by PraveenKS

I am new to TestNG and Selenium Webdriver and trying to create a testng.xml using eclipse.

我是 TestNG 和 Selenium Webdriver 的新手,并尝试使用 Eclipse 创建一个 testng.xml。

Done some research in www and understand that this file can be created by doing Right Click on the Java class >> TestNG >> Convert to TestNG

在 www 上做了一些研究,并了解到可以通过右键单击 Java 类 >> TestNG >> 转换为 TestNG 来创建此文件

However this Convert Refactoring wizard is not opening.

然而,这个转换重构向导没有打开。

Whether this wizard will not open, if the Java class already contains @Test annotations (!?)

如果 Java 类已经包含 @Test 注释(!?),此向导是否不会打开

Can you help me to create testng.xml using eclipse please.

你能帮我用eclipse创建testng.xml吗?

(I am using Eclipse Luna)

(我正在使用 Eclipse Luna)

采纳答案by PraveenKS

Created testng suite file manually as below:

手动创建 testng 套件文件如下:

  1. Run once your project from Eclipse
  2. Refresh your project
  3. Now, “test-output” folder will be created under your project –> unfold the folder
  4. Double click the ‘index.html' file -> It will be opened in the Eclipse Browser window or any other external browser (e.g. IE, Google Chrome, FF, MS Edge etc.)
  5. Click on the ‘.xml' link given on the left panel
  6. The test suite XML will get opened on the right-side panel
  7. Take a copy of the XML content
  8. Right click your project >> New >> File
  9. Give name of your test suite file (with .xml extension e.g. testng.xml) and click Finish
  10. Paste the content that you copied from the ‘index.html' and amend your suite file as per your needs
  1. 从 Eclipse 运行一次您的项目
  2. 刷新你的项目
  3. 现在,将在您的项目下创建“test-output”文件夹 -> 展开文件夹
  4. 双击“index.html”文件 -> 它将在 Eclipse 浏览器窗口或任何其他外部浏览器(例如 IE、Google Chrome、FF、MS Edge 等)中打开
  5. 单击左侧面板上给出的“.xml”链接
  6. 测试套件 XML 将在右侧面板上打开
  7. 复制 XML 内容
  8. 右键单击您的项目 >> 新建 >> 文件
  9. 给出您的测试套件文件的名称(带有 .xml 扩展名,例如 testng.xml)并单击完成
  10. 粘贴您从“index.html”复制的内容,并根据您的需要修改您的套件文件

回答by Ram Pasala

Praveen, please right click on the project and then go to convert to testNG,

Praveen,请在项目上右击,然后去转换为testNG,

now it will create testng.xml in which you will have all your classes, you can select the classes which u want and then execute this testng.xml as testng suite.

现在它将创建 testng.xml,您将在其中拥有所有类,您可以选择您想要的类,然后将此 testng.xml 作为 testng 套件执行。

回答by Arpan Saini

  1. Click on Project
  2. CNTL+ENTER
  3. Go to general file
  4. Create a file in your project with name testng.xml
  5. click on finish
  6. Open that file and write the given below code
  1. 点击项目
  2. CNTL+回车
  3. 转到一般文件
  4. 在您的项目中创建一个名为 testng.xml 的文件
  5. 点击完成
  6. 打开该文件并编写下面给出的代码

Change the names according to your program and run that testng.xml Run as Testng it will run your program with Testng

根据您的程序更改名称并运行 testng.xml Run as Testng 它将使用 Testng 运行您的程序

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite Name"  > 
<!-- suite name="Suite Name" --> 


             <test name="TestA" allow-return-values="true">
                     <classes>
                       <!-- packagename.Testcase class name  -->
                             <class name ="listnerDemo.Testcases" />
                     </classes>
             </test>


                   <listeners>
              <!-- packagename.customerlistner_classname  -->
                       <listener class-name="listnerDemo.CustomerListner"/>
              </listeners>

</suite>

*********************************if you have to run using multiple suite********

*************************************如果您必须使用多个套件运行********

let me give you an example if you have three suites that have multiple cases then create the tree .xml file and write the code as above for all the three suite

让我举个例子,如果你有三个套件有多个案例然后创建树 .xml 文件并为所有三个套件编写上述代码

then create a testng.xml file that will include the code as below

然后创建一个包含以下代码的 testng.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<Suite name="Data driver testng project">
<Suite-files>
    <Suite-files path="C:\Arpan\Study Documents\Selenium\Selenium Framework\Core_Framework_TestNG\suiteA.xml" />
    <Suite-files path="C:\Arpan\Study Documents\Selenium\Selenium Framework\Core_Framework_TestNG\suiteB.xml" />
    <Suite-files path="C:\Arpan\Study Documents\Selenium\Selenium Framework\Core_Framework_TestNG\suiteC.xml" />
</Suite-files>
</Suite>

Then run the testng.xml Run as Testng then it will run all the program of those are part of these three suites

然后运行 ​​testng.xml Run as Testng 然后它会运行这三个套件中的所有程序

回答by Amit Pal

  • Step 1 : Select your project folder in which you have all the packages.
  • Step 2 : Right click on selected project folder
  • Step 3 : Mouse hover on TestNG option from the given list which is located at the bottom.
  • Step 4 : Select "Convert to TestNG" option
  • Step 5 : A modal will appear with the name of "Generate testng.xml"
  • Step 6 : Click on Finish button to complete the process.
  • 第 1 步:选择包含所有包的项目文件夹。
  • 第 2 步:右键单击选定的项目文件夹
  • 第 3 步:将鼠标悬停在位于底部的给定列表中的 TestNG 选项上。
  • 第 4 步:选择“转换为 TestNG”选项
  • 第 5 步:将出现一个名为“Generate testng.xml”的模式
  • 第 6 步:单击“完成”按钮以完成该过程。