TestNG:@Parameters不起作用

问题描述 投票:1回答:3

我已经开始学习TestNG了,这段代码对我不起作用:

package com.automation;

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

public class TestNg_ParameterTestClass {

    @Parameters({"Param1","Param2"})
    @Test(enabled = true)
    public void testTestExample(String p1, String p2){
        System.out.println("Parameter's value : " + p1 + ", " + p2);
    }
}

testng.xml为空,因为我直接从IntelliJ运行测试方法,每次收到此消息时:

Test ignored.
Test ignored.
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 1
===============================================

你知道出了什么问题,为什么testNG会跳过它?

谢谢你的帮助,拉法尔

java intellij-idea testng
3个回答
3
投票

这个问题的答案相当简单。

@Parameters({“Param1”,“Param2”}),Param1和Param2是testng.xml中参数的名称,这些参数不是在运行时分配给String p1,String p2的值。

必须定义两个XML参数,否则将忽略测试。您可以使用“可选”注释定义自动分配给p1和p2的可选值:

@Parameters({"param1","param2"})
@Test(enabled = true)
public void testTestExample(@Optional("test1111") String p1, @Optional("test2222")String p2){
    System.out.println("Parameter's value : " + p1 + ", " + p2);
}

3
投票

TestNG正在跳过您的测试,因为您没有将强制参数传递给它。

您应该可以通过IntelliJ的Run/Debug Configurations菜单来完成。

否则你必须使用@Optional


0
投票

您需要在Testng.xml中定义这两个参数(Param1,Param2),然后才能在测试类中使用它。

您需要正确定义testng.xml以使用@Parameters

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<parameter name="Param1" value="Value_Of_Param1"/>
<parameter name="Param2" value="Value_Of_Param2"/>
<classes>
  <class name="CLASS_NAME"/> 
 </classes>
 </test> <!-- Test -->
 </suite> <!-- Suite -->

检查第5和第6行,您将获得解决方案。希望它有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.