英文:
TestNG parametrization pass in method
问题
我想知道在使用testng.xml时是否可以在方法中传入参数。我知道可以在测试类中设置参数。我使用页面对象模型方法。以下是我的代码示例:
<suite name="dev-parametrization" verbose="1">
<test name="Paragraphs-Tests">
<classes>
<class name="com.java.tests.ParagraphsApiControllerTests">
<methods>
<include name="createParagraph">
<parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
</include>
</methods>
</class>
</classes>
</test>
</suite>
以下是测试类:
public class ParagraphsApiControllerTests {
Paragraphs paragraphs = new Paragraphs();
@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void createParagraph() {
paragraphs.createParagraph();
}
以及我的方法 - 在这里,我想使用来自XML文件的参数。这可能吗?我该如何做到这一点?
public class Paragraphs {
String paragraphsURL = "http://192.168.0.139:8880/paragraphs";
String apiParagraphsURL = "http://192.168.0.139/api/paragraphs";
public void createParagraph() {
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
<suite name="dev-parametrization" verbose="1" >
<test name="Paragraphs-Tests">
<classes>
<class name="com.java.tests.ParagraphsApiControllerTests">
<methods>
<include name="createParagraph">
<parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
</include>
</methods>
</class>
</classes>
</test>
Below test class
public class ParagraphsApiControllerTests {
Paragraphs paragraphs = new Paragraphs();
@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void createParagraph() {
paragraphs.createParagraph();
}
And my method - here I want to use parameter from xml. file. Is it possible? How can I do this?
public class Paragraphs {
String paragraphsURL = "http://192.168.0.139:8880/paragraphs";
String apiParagraphsURL = "http://192.168.0.139/api/paragraphs";
public void createParagraph() {
RestAssured.baseURI = paragraphsURL;
答案1
得分: 1
不要在测试类和Paragraph
类中使用相同的方法名。我将测试类方法名从createParagraph
更改为testCreateParagraph
。
Testng.xml
<suite name="dev-parametrization" verbose="1">
<test name="Paragraphs-Tests">
<classes>
<class name="com.java.tests.ParagraphsApiControllerTests">
<methods>
<include name="testCreateParagraph">
<parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
</include>
</methods>
</class>
</classes>
</test>
</suite>
测试类
public class ParagraphsApiControllerTests {
Paragraphs paragraphs = new Paragraphs();
@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void testCreateParagraph(String paragraphsURL) {
paragraphs.createParagraph(paragraphsURL);
}
}
段落类
public class Paragraphs {
public void createParagraph(String paragraphsURL) {
RestAssured.baseURI = paragraphsURL;
}
}
更多信息,请参阅此教程。
英文:
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
<suite name="dev-parametrization" verbose="1" >
<test name="Paragraphs-Tests">
<classes>
<class name="com.java.tests.ParagraphsApiControllerTests">
<methods>
<include name="testCreateParagraph">
<parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
</include>
</methods>
</class>
</classes>
</test>
Test class
public class ParagraphsApiControllerTests {
Paragraphs paragraphs = new Paragraphs();
@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void testCreateParagraph(String paragraphsURL) {
paragraphs.createParagraph(paragraphsURL);
}
Paragraph class
public class Paragraphs {
public void createParagraph(String paragraphsURL) {
RestAssured.baseURI = paragraphsURL;
}
}
Refer this tutorial for more information
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论