testNG分组执行的时候报错depends on nonexistent method “xxxx.xxxxx“

Posted 这个博主狠弱?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了testNG分组执行的时候报错depends on nonexistent method “xxxx.xxxxx“相关的知识,希望对你有一定的参考价值。

错误的代码:

package pro.data;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import pro.assertion.MyAssert;

//sb124--jenkis
public class AddFund {
	CloseableHttpClient client = null;
	CloseableHttpResponse response = null;
	MyAssert assertion = new MyAssert();
	private int i = 5;


	@Parameters({ "myNumber" })
	public AddFund(@Optional("5") int i) {
		this.i = i;
	}

	@BeforeClass
	public void beforeClass() {
		client = HttpClients.createDefault();
		System.out.println("beforeClass-hc");
	}

	@Test(testName = "login")
	public void login() {
		System.out.println("i 先登录");
	}

	@Parameters({ "myNumber" })
	@Test(dependsOnMethods = {"login" }, groups = { "ga" })
	public void add(String userId, String fundCode, String fundName, String holdPortion) {
		System.out.println("执行第" + i + "次");
		String content = "";
		HttpGet request = new HttpGet("http://localhost:9999");
		try {
//			此处可以解决中文乱码
			fundName = URLEncoder.encode(fundName, "utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String path = "/addFundRpWithParams?userId=" + userId + "&fundCode=" + fundCode + "&fundName=" + fundName
				+ "&holdPortion=" + holdPortion;
		request.setPath(path);
		try {
			System.out.println(request.getRequestUri());
			response = client.execute(request);
			HttpEntity entity = response.getEntity();
			content = EntityUtils.toString(entity, "utf-8");
			// System.out.println(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		assertion.assertEquals(content.contains("成功"), true);
		assertion.assertAll();
	}
	@Test(groups= {"gb"})
	public void pr() {
		System.out.println("当前线程id:"+Thread.currentThread().getId());
	}

}
<test name="add-fund" group-by-instances="true">
		<packages>
			<package name="pro.data" />
		</packages>
		<groups>
			<run>
				<include name="ga" />
			</run>
		</groups>
</test>

可以看出方法add所在的组为“ga",并且依赖方法login,不过login并未处在任何分组中,通过xml执行的时候,login方法不在执行范围,所以导致org.testng.TestNGException--depends on nonexistent method "pro.data.AddFund.login" 错误

解决办法:

只要将依赖的放也放进被执行的组中即可

	@Test(testName = "login",groups = { "ga" })
	public void login() {
		System.out.println("i 先登录");
	}

以上是关于testNG分组执行的时候报错depends on nonexistent method “xxxx.xxxxx“的主要内容,如果未能解决你的问题,请参考以下文章

testng运行报错 请问是为啥

testng.xml的运行机制部分总结

自动化测试由浅入深--TestNg测试用例的分组

TestNG依赖测试

Eclipse集成testng插件(离线安装方式)

testNg 关闭浏览器异常解决办法