testng 数据驱动
Summary: The point of this article is to understand how to succeed at passing data to classes via .xlsx file.
简介:本文的重点是了解如何成功通过.xlsx文件将数据传递给类。
The DataProviders in TestNG are another way to pass the parameters in the test function or aka TestNG parameters. DataProviders pass different values to the TestNG Test Case via TestNG Annotations. It is embedded in TestNG data-driven testing.
TestNG中的DataProvider是传递测试函数中的参数(也称为TestNG参数)的另一种方法。 数据提供者通过TestNG注释将不同的值传递给TestNG测试用例。 它嵌入在TestNG数据驱动的测试中。
With that said, when it comes to creating automation testing scenarios, it is always good to test all data validation fields within a web application. Hardcoding various inputs to the methods over and over again can work, but it is not ideal. To save some time DataProviders help in passing the parameters in different ways. Depending on the project preferences, some people may use .xml file types to execute this action. However, I will use .xlsx file type to illustrate the expected behaviors and nuances.
话虽如此,在创建自动化测试方案时,最好测试Web应用程序中的所有数据验证字段。 一遍又一遍地对方法的各种输入进行硬编码是可行的,但这并不理想。 为了节省时间,DataProviders帮助以不同的方式传递参数。 根据项目的首选项,某些人可能会使用.xml文件类型来执行此操作。 但是,我将使用.xlsx文件类型来说明预期的行为和细微差别。
Import .xlsx to existing test automation project
将.xlsx导入现有的测试自动化项目
Assuming there is an existing project within your Eclipse or any type of Integrated Development Environment(IDE) in use, we want to import an existing .xlsx from your Operating System (OS) to the IDE.
假设您的Eclipse中有一个现有项目或正在使用任何类型的集成开发环境(IDE),我们想将一个现有.xlsx从您的操作系统(OS)导入到IDE。
2. Setting up parameters in .xlsx
2.在.xlsx中设置参数
Once the file is imported from your PC, the idea is to populate .xlsx object and open it within the IDE. Please be sure to have a clean file. What I mean by this is to leave all formatting out such as alignment preferences, colors, macro script and all of that. Java or whatever language it is may only prefer to read plain text when using the “@DataProvider” annotation. The whole idea is to establish a spreadsheet for values to pass through a string established in a class method. I cannot stress this enough.
从PC导入文件后,想法是填充.xlsx对象并在IDE中打开它。 请确保文件干净。 我的意思是忽略所有格式,例如对齐方式首选项,颜色,宏脚本等。 Java或任何其他语言可能仅在使用“ @DataProvider”注释时更喜欢阅读纯文本。 整个想法是建立一个电子表格,让值通过在类方法中建立的字符串传递。 我不能太强调这一点。
3. Consistency
3.一致性
The strings established within the .xlsx should correspond with the strings established with the targeted class invoking the “@DataProvider” annotation. Please see the snapshot above. It will show how I have structured the class to be nested within the hierarchy. It will then progress to how I have established a path for the test object array to be read from the .xlsx object. Ultimately, it will invoke the given annotation for values to be passed from the file.
在.xlsx中建立的字符串应与在目标类上调用“ @DataProvider”注释的目标建立的字符串相对应。 请查看上面的快照。 它将显示我如何构造要嵌套在层次结构中的类。 然后,它将继续介绍如何为要从.xlsx对象读取的测试对象数组建立路径。 最终,它将调用给定的注释,以从文件传递值。
4. Running the class
4.上课
Assuming that all the steps have been followed, one should be able to run the class with no issues. If there are any complications, it should thrown an exception error. Remember, the strings generally should not change. However, the values can be modified to generate various data driven results of the executed test.
假定已遵循所有步骤,则应该可以毫无问题地运行该类。 如果有任何并发症,应该抛出异常错误。 请记住,字符串通常不应更改。 但是,可以修改这些值以生成已执行测试的各种数据驱动结果。
Conclusion:
结论:
Automation test does not have to be too painful. Just like any aspect in programming and in life, recognizing the small nuances and understanding the error of ways are the secret to any success. I know that I have struggled with this aspect of utilizing the TestNG in conjunction with the Selenium framework in the past. I am sure that if I can get a grip on this, so can you. If not, then have somebody else do it for you. That always works too :-).
自动化测试不必太痛苦。 就像编程和生活中的任何方面一样,认识到细微差别并理解方式错误是成功的秘诀。 我知道过去我一直在尝试将TestNG与Selenium框架结合使用。 我相信,如果我能对此有所了解,那么您也可以。 如果没有,那就请别人为您做。 这也总是可行的:-)。
翻译自: https://medium.com/the-innovation/testng-dataproviders-how-to-make-it-work-each-time-5b41b8d024cb
testng 数据驱动