Parameter 'Name' is required by @Test on m

2019-08-09 05:21发布

Here is my selenium testng script and XML file. I am getting the error message. Please help me how to proceed this

Err MSG : Parameter 'Name' is required by @Test on method parametertest but has not been marked @Optional

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Sampleparameterized {

@Test
@Parameters("Name")
public void parametertest(String Name) {
System.out.println("Parameterized value is " +Name);
}
}

XML file is

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<parameter name = "Name" value ="TOM"/>

<classes>
<class name="Testing.Sampleparameterized"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

5条回答
We Are One
2楼-- · 2019-08-09 05:30

This may be happening when you run from eclipse as Single TestNG test. You may want to refer SO answer given already on TestNG @Optional parameter error.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-09 05:30

This error normally comes when you are trying to run individual TestNg class instead of run *.xml. Your parameter is set in *.xml file, so right click on *.xml file and Run as TestNg. Make sure your TestNg class is properly mapped in your *.xml file (class name="com.test.WebServices"). Here WebServices.java is your TestNg class.

查看更多
欢心
4楼-- · 2019-08-09 05:32

You would need to add the following in order to be able to run the test directly from the test class: @Optional("Name").

For example:

public void parametertest(@Optional("Name"),String Name) {
System.out.println("Parameterized value is " +Name);
}

Works well for me! Let me know how it goes for you.

查看更多
ら.Afraid
5楼-- · 2019-08-09 05:36

Simply run you testclass from *.xml file (right click on *.xml -> Run). Parameterized test can't be run directly.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-08-09 05:40

I was facing same issue which got resolved by below steps -
1. Set your xml file in Project->Properties->TestNG->Template XML File
2. Right click on your Java file and choosing Run As...->TestNG Test (Thus template will be used)

查看更多
登录 后发表回答