java @Test 需要参数但尚未标记@optional 或定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45024793/
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
parameter is required by @ Test but has not been marked @optional or defined
提问by dss
Hi I am seeing this error, please help
嗨,我看到这个错误,请帮忙
Parameter 'Visit' is required by @Test on method searchByVisitNo but has not been marked @Optional or defined.
方法 searchByVisitNo 上的 @Test 需要参数“访问”,但尚未标记为 @Optional 或定义。
I don't know why is there a need to mark it optional when it is defined in testng xml file
我不知道为什么在testng xml文件中定义时需要将其标记为可选
Here is the entire code I used
这是我使用的整个代码
<suite name="Suite" parallel="tests">
<test name="SearchByVisit">
<parameter name="Visit" value="123456"/>
<classes>
<class name="abc"/>
</classes>
</test>
</suite>
@Parameters({"Visit"})
@Test(priority=3)
public void searchByVisitNo(String VisitNumber)throws InterruptedException
{
searchByVisit(VisitNumber);
}
public void searchByVisit(String Visit) throws InterruptedException
{
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("search-input"))
);
element.sendKeys(Visit);
clickSearch();
}
回答by Ankur Singh
You are passing paramter <parameter name="Visit" value="123456"/>
in your .xml file and you directly running your TestNG class. So it's not getting that parameter while compiling .
您<parameter name="Visit" value="123456"/>
在 .xml 文件中传递参数并直接运行 TestNG 类。所以它在编译时没有得到那个参数。
So you need to run your xml suite to provide a valid parameter to your TestNG class.
所以你需要运行你的 xml 套件来为你的 TestNG 类提供一个有效的参数。
回答by Monika
You are getting this error as you are directly running this class. You should run your TestNG XML file.
当您直接运行此类时,您会收到此错误。您应该运行您的 TestNG XML 文件。
Steps which you can follow:
您可以遵循的步骤:
- Create a test suite.
- Specify class and method name(Class name should be packageName.className)
- Specify the parameters which should be used for a method.
Run the test suite.
Looks like you are directly trying to run the class hence it is showing that exception.
- 创建一个测试套件。
- 指定类和方法名(类名应该是packageName.className)
- 指定应该用于方法的参数。
运行测试套件。
看起来您是直接尝试运行该类,因此它显示了该异常。
回答by Jayasmita B
In your TestNG.xml file, you need to provide your test class name and in class name you need to provide the class name along with package. That would resolve the issue!
在您的 TestNG.xml 文件中,您需要提供您的测试类名,并且您需要在类名中提供类名和包。这样问题就解决了!
Consider below example for reference :
考虑以下示例以供参考:
If the test class name is Testclass and it's under package com.testpackage, then your testNG.xml file would look like below:
如果测试类名称是 Testclass 并且它在包 com.testpackage 下,那么您的 testNG.xml 文件将如下所示:
<suite name="Suite" parallel="tests">
<test name="Testclass">
<parameter name="Visit" value="123456"/>
<classes>
<class name="com.testpackage.Testclass"/>
</classes>
</test>
</suite>
回答by Praful Chikkamath
Even I was facing the same issue the problem simple ..
即使我面临同样的问题,问题也很简单..
- In Testng XML I was passing the parameters in the day1 code instead of day4
- Pass these all parameter to correct test cases folder in testng XML
- 在 Testng XML 中,我在 day1 代码而不是 day4 中传递参数
- 将这些所有参数传递给 testng XML 中的正确测试用例文件夹
Consider belows Day1 code and Day4 code two are there
考虑下面有第 1 天代码和第 4 天代码两个
In Testng xml passing the parameter in day4 instead of day1 will throw this error
在 Testng xml 中,在 day4 而不是 day1 中传递参数将引发此错误
Day 1
第一天
package test2;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Day1 {
@Parameters({"URL"}) //Only to Particular test method only
@Test
public void test11(String urlname)
{
System.out.println(urlname);
System.out.println("Test case 11 in Day1");
}
@Test
public void test12()
{
System.out.println("Test case 12 in Day1");
}
@Test
public void test13()
{
System.out.println("Test case 13 in Day1\n");
}
}
Day4
第4天
package test2;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Day4 {
@Parameters({"URL","API/username"})
@Test
public void test41(String urlname, String name)
{
System.out.println(urlname);
System.out.println("Test case 41 in Day4");
System.out.println(name);
}
@Test
public void test42()
{
System.out.println("Test case 42 in Day4");
}
}
XML ERROR
XML 错误
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name = "URL" value = "https://www.youtube.com"/>
<**test name = "Day2Testcase">
<parameter name = "URL" value = "day2test2.com"/>
<parameter name = "API/username" value = "12345"/>**
<classes>
<class name = "test2.Day1">
</class>
</classes>
</test>
<test name = "Day4Testcase">
<parameter name = "URL" value = "day4test2.com"/>
<classes>
<class name = "test2.Day4">
</class>
</classes>
</test>
ERROR
错误
[Utils] [ERROR] [Error] org.testng.TestNGException: Parameter 'API/username' is required by @Test on method test41 but has not been marked @Optional or defined in F:\Selenium WebDriver with Java -Basics to Advanced+Frameworks by Rahul Shetty\Workspace\TestNG_Tutorial\testng.xml
[Utils] [ERROR] [Error] org.testng.TestNGException: 方法 test41 上的 @Test 需要参数“API/用户名”,但尚未标记为 @Optional 或在 F:\Selenium WebDriver 中使用 Java -Basics to Advanced 定义+框架由 Rahul Shetty\Workspace\TestNG_Tutorial\testng.xml
Corrected the XML
更正了 XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name = "Day2Testcase">
<parameter name = "URL" value = "day2test2.com"/>
<classes>
<class name = "test2.Day1">
</class>
</classes>
</test>
<test name = "Day4Testcase">
<parameter name = "URL" value = "day4test2.com"/>
<parameter name = "username" value = "12345"/>
<classes>
<class name = "test2.Day4">
</class>
</classes>
</test>
</suite> <!-- Suite -->
回答by Binnu Jesudasan Gudapati
Seems the issue is with using curly-braces. I had tried with below snippet:
似乎问题在于使用花括号。我曾尝试使用以下代码段:
@Parameters({"browser"})
@BeforeMethod
public void setUp(String browser){
if(browser.equalsIgnoreCase("Mozilla Firefox")){
driver = new FirefoxDriver();
}
was getting the below error:
收到以下错误:
Parameter '{browser}' is required by BeforeMethod on method setUp but has not been marked @Optional or defined
方法 setUp 上的 BeforeMethod 需要参数“{browser}”,但尚未标记为 @Optional 或定义
It started working after removing the curly-braces as below:
它在删除花括号后开始工作,如下所示:
@Parameters("browser")
回答by Nitin Dubey
You should try updating your pom with below. Simply add the below in your pom and then execute. Add it just above dependencies tag.
您应该尝试使用以下内容更新您的 pom。只需在您的 pom 中添加以下内容,然后执行。将其添加到依赖项标记上方。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
回答by Ashish Sonkamble
Mention below line at the start of your XML file:
在 XML 文件的开头提及以下行:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
回答by Tejashree Kutte
You need to add @Optional("Abc") in your method, which specifies that the current parameter is optional. TestNG will pass in a specified default value, or null if none is specified.
您需要在您的方法中添加@Optional("Abc"),它指定当前参数是可选的。TestNG 将传入指定的默认值,如果未指定则为 null。
Try doing public void searchByVisit(@Optional("Abc") String Visit) throws InterruptedException
尝试做 public void searchByVisit(@Optional("Abc") String Visit) throws InterruptedException
回答by SUPARNA SOMAN
Are you using a Maven repo? Try running your pom.xml as Maven clean and then Maven test. This worked for me
你在使用 Maven 仓库吗?尝试将 pom.xml 作为 Maven clean 运行,然后 Maven 测试。这对我有用
回答by Hassan Ali
After Opening browser, no URL is being passed and it last with Error which says
"Parameter 'browser' is required by BeforeTest on method setup but has not been marked @Optional or defined
in C:\Users\hassan.anwer\IdeaProjects\CRBTesting\testing.xml"
打开浏览器后,没有 URL 被传递,最后出现错误,提示“方法设置时 BeforeTest 需要参数‘浏览器’,但尚未标记 @Optional 或在 C:\Users\hassan.anwer\IdeaProjects\CRBTesting\testing.xml"