TestNG参数化在方法中传递

huangapple go评论94阅读模式
英文:

TestNG parametrization pass in method

问题

我想知道在使用testng.xml时是否可以在方法中传入参数。我知道可以在测试类中设置参数。我使用页面对象模型方法。以下是我的代码示例:

  1. <suite name="dev-parametrization" verbose="1">
  2. <test name="Paragraphs-Tests">
  3. <classes>
  4. <class name="com.java.tests.ParagraphsApiControllerTests">
  5. <methods>
  6. <include name="createParagraph">
  7. <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
  8. </include>
  9. </methods>
  10. </class>
  11. </classes>
  12. </test>
  13. </suite>

以下是测试类:

  1. public class ParagraphsApiControllerTests {
  2. Paragraphs paragraphs = new Paragraphs();
  3. @Parameters({"paragraphsURL"})
  4. @Test(priority = 1)
  5. public void createParagraph() {
  6. paragraphs.createParagraph();
  7. }

以及我的方法 - 在这里,我想使用来自XML文件的参数。这可能吗?我该如何做到这一点?

  1. public class Paragraphs {
  2. String paragraphsURL = "http://192.168.0.139:8880/paragraphs";
  3. String apiParagraphsURL = "http://192.168.0.139/api/paragraphs";
  4. public void createParagraph() {
  5. RestAssured.baseURI = paragraphsURL;
英文:

I'm wondering is it possible to put parameters in method when I use testng.xml. I know about put parameteres in test class. I use page object model approach. Here is my code

  1. &lt;suite name=&quot;dev-parametrization&quot; verbose=&quot;1&quot; &gt;
  2. &lt;test name=&quot;Paragraphs-Tests&quot;&gt;
  3. &lt;classes&gt;
  4. &lt;class name=&quot;com.java.tests.ParagraphsApiControllerTests&quot;&gt;
  5. &lt;methods&gt;
  6. &lt;include name=&quot;createParagraph&quot;&gt;
  7. &lt;parameter name=&quot;paragraphsURL&quot; value=&quot;http://192.168.0.139:8880/paragraphs&quot;/&gt;
  8. &lt;/include&gt;
  9. &lt;/methods&gt;
  10. &lt;/class&gt;
  11. &lt;/classes&gt;
  12. &lt;/test&gt;

Below test class

  1. public class ParagraphsApiControllerTests {
  2. Paragraphs paragraphs = new Paragraphs();
  3. @Parameters({&quot;paragraphsURL&quot;})
  4. @Test(priority = 1)
  5. public void createParagraph() {
  6. paragraphs.createParagraph();
  7. }

And my method - here I want to use parameter from xml. file. Is it possible? How can I do this?

  1. public class Paragraphs {
  2. String paragraphsURL = &quot;http://192.168.0.139:8880/paragraphs&quot;;
  3. String apiParagraphsURL = &quot;http://192.168.0.139/api/paragraphs&quot;;
  4. public void createParagraph() {
  5. RestAssured.baseURI = paragraphsURL;

答案1

得分: 1

不要在测试类和Paragraph类中使用相同的方法名。我将测试类方法名从createParagraph更改为testCreateParagraph

Testng.xml

  1. <suite name="dev-parametrization" verbose="1">
  2. <test name="Paragraphs-Tests">
  3. <classes>
  4. <class name="com.java.tests.ParagraphsApiControllerTests">
  5. <methods>
  6. <include name="testCreateParagraph">
  7. <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
  8. </include>
  9. </methods>
  10. </class>
  11. </classes>
  12. </test>
  13. </suite>

测试类

  1. public class ParagraphsApiControllerTests {
  2. Paragraphs paragraphs = new Paragraphs();
  3. @Parameters({"paragraphsURL"})
  4. @Test(priority = 1)
  5. public void testCreateParagraph(String paragraphsURL) {
  6. paragraphs.createParagraph(paragraphsURL);
  7. }
  8. }

段落类

  1. public class Paragraphs {
  2. public void createParagraph(String paragraphsURL) {
  3. RestAssured.baseURI = paragraphsURL;
  4. }
  5. }

更多信息,请参阅此教程

英文:

Don't use the same method name in test class and in Paragraph class. I changed the test class method name from createParagraph to testCreateParagraph.

Testng.xml

  1. &lt;suite name=&quot;dev-parametrization&quot; verbose=&quot;1&quot; &gt;
  2. &lt;test name=&quot;Paragraphs-Tests&quot;&gt;
  3. &lt;classes&gt;
  4. &lt;class name=&quot;com.java.tests.ParagraphsApiControllerTests&quot;&gt;
  5. &lt;methods&gt;
  6. &lt;include name=&quot;testCreateParagraph&quot;&gt;
  7. &lt;parameter name=&quot;paragraphsURL&quot; value=&quot;http://192.168.0.139:8880/paragraphs&quot;/&gt;
  8. &lt;/include&gt;
  9. &lt;/methods&gt;
  10. &lt;/class&gt;
  11. &lt;/classes&gt;
  12. &lt;/test&gt;

Test class

public class ParagraphsApiControllerTests {

  1. Paragraphs paragraphs = new Paragraphs();
  2. @Parameters({&quot;paragraphsURL&quot;})
  3. @Test(priority = 1)
  4. public void testCreateParagraph(String paragraphsURL) {
  5. paragraphs.createParagraph(paragraphsURL);
  6. }

Paragraph class

  1. public class Paragraphs {
  2. public void createParagraph(String paragraphsURL) {
  3. RestAssured.baseURI = paragraphsURL;
  4. }
  5. }

Refer this tutorial for more information

huangapple
  • 本文由 发表于 2020年10月8日 19:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64261691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定