gpt4 book ai didi

java - java中从testng到jar的转换显示错误

转载 作者:行者123 更新时间:2023-12-02 12:21:27 26 4
gpt4 key购买 nike

我通过创建一个 Main 类并将 testng 文件转换为 jar,并在其中包含 main 方法

package uatFramework;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;

public class Mainone{
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { RMS_Login.class });
testng.addListener(tla);
testng.run();
}
}

但是 RMS_Login 类也扩展了 BeforeSfterSuite 类。现在,当我将 main 作为 java 应用程序运行时 -> 成功打开和关闭浏览器,但页面未加载,并且出现以下错误:

@Parameters({"sUsername","sPassword","sUrl"})       
public void uatRMSLoginTest(String sUsername, String sPassword,java.lang.String sUrl) throws InterruptedException, IOException
{
FileWriter writer = new FileWriter(file,true);
String filename= "screenshots";
File filepath =new File(folderpath+"/" +filename);
filepath.mkdirs();
if (System.getenv("sURL") != null)
{
....
}
}

作为 org.testng.TestNGException:uatRMSLoginTest 方法上的 @Test 需要参数“sUsername”,但尚未标记为 @Optional 或已定义 在 uatFramework.Mainone.main(Mainone.java:13) 即 main 方法中的 testng.run

最佳答案

您的代码的问题在于,它没有通过 @Parameters 注释注入(inject)您的 @Test 方法所期望的所需参数。

这是一个代码,其中包含工作代码。

import org.testng.ITestNGListener;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.Collections;

public class Mainone {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();

XmlSuite xmlSuite = new XmlSuite();
xmlSuite.getParameters().put("sUsername", "admin");
xmlSuite.getParameters().put("sPassword", "admin");
xmlSuite.getParameters().put("sUrl", "http://localhost");
xmlSuite.setName("Sample_Suite");
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("Sample_Test");
xmlTest.setClasses(Collections.singletonList(new XmlClass(RMS_Login.class)));
testng.addListener((ITestNGListener) tla);
testng.setXmlSuites(Collections.singletonList(xmlSuite));
testng.run();
}

public static class RMS_Login {

@Parameters({"sUsername", "sPassword", "sUrl"})
@Test
public void uatRMSLoginTest(String sUsername, String sPassword, String sUrl) {
//Since I don't know what this method is supposed to do,
//Just printing them out.
System.err.println("UserName " + sUsername);
System.err.println("Password " + sPassword);
System.err.println("URL " + sUrl);
}
}
}

这是输出

UserName admin
Password admin
URL http://localhost

===============================================
Sample_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

关于java - java中从testng到jar的转换显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45755991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com