测试 |从可运行 jar 中的 main 方法调用 testng.xml 文件

TESTNG | call testng.xml file from main method from inside a runnable jar

我是 testng + maven 的新手,我需要 运行 使用 运行nable jar 的脚本。该套件需要针对多个区域执行。区域名称在执行 jar 时作为参数传递。 现在我的要求是根据提供的区域名称在多个 testng.xml 文件之间切换。 所有 testng.xml 文件都放在资源文件夹中 --> "resources/testngA.xml" 和 "resources/testngB.xml"。 当我 运行 来自 eclipse 的脚本时,它工作正常,但是当我尝试通过 runnable.jar 执行相同的脚本时,它显示 java.io.FileNotFoundException 异常 。谁能帮我解决这个问题。

public class TestRunner {

    static TestNG testng;
    static List<String> suites;
    public static void main(String[] args) {
        String xmlFileName = "";
        List<XmlSuite> suite;
        String region = arg[0];
        try
        {
            if(region.equals("A")){
                xmlFileName = "resources/A_TestNG.xml";
            }else if(region.equals("B")){
                xmlFileName = "resources/B_TestNG.xml";
            }else{
                System.out.println("No matching region found")
            }
            suite = (List <XmlSuite>)(new Parser(xmlFileName).parse());
            testng.setXmlSuites(suite);
            testng.run();
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

当您处理资源时,您应该使用以下方法:

1) 取资源URI

URI aTestNgURI = TestRunner.class.getClassLoader().getResource("A_TestNG.xml").toURI();

2) 获取文件路径

String aTestNgFilePath = new File(aTestNgURI).getAbsolutePath();