测试参考

ContiPerf

ContiPerf 性能测试工具
轻量级测试工具ContiPerf
导包:org.databene#contiperf
核心类:ContiPerfRule;@PerfTest、@Required

使用

@RunWith(SpringRunner.class);标记类
@Rule public ContiPerfRule cpr=new ContiPerfRule();启用规则
@PerfTest;执行过程参数。线程数、执行次数、执行时间等。
@Required;执行结果限制。执行时间、每秒执行数等。
测试结果会自动生成html,位于target/contiperf-result。

public class ContiPerfTest {
	@Rule
	public ContiPerfRule i = new ContiPerfRule();

	@Test
	@PerfTest(invocations = 1000, threads = 40)
	@Required(max = 1200, average = 250, totalTime = 60000)
	public void test1() throws Exception {
		Thread.sleep(200);
	}
}

Junit4

官网
Junit5教程

网络教程

测试方法、测试类、测试集、测试运行器
JUnit4教程+实践
JUnit4教程(一):基本应用

常用注解

@Test标记测试方法,属性:timeout、expected
@Ignore忽略测试
@Before、@After测试方法回调,每个测试方法都调用
@BeforeClass、@AfterClass测试类回调,标记public static void mName(){}
@RunWith测试运行器,默认JUnit4.class,其他:Parameterized、Suite、Theories等
@Parameters标记参数生成方法,签名:public static Collection mName(){}
@SuiteClasses测试集

Parameterized测试

参数化测试

  • @RunWith(Parameterized.class);标记类
  • 声明成员变量,带参构造器(或@Parameter标记参数)
  • @Parameterized.Parameters标记public static方法;返回Iterable、数组(元素作为一组构造器参数)
@RunWith(Parameterized.class)
public class ParameterizedTest {
	static String token;
	Object result;

	@BeforeClass
	public static void init() {
		token = "init token";
	}

	@After
	public void printResult() {
		System.out.println(result);
	}
	
	String str; 
	Integer num;
	public ParameterizedTest(String str, Integer num) {
		this.str=str;
		this.num=num;
	}
//	@Parameterized.Parameters
//	public static Collection p() {
//		return Arrays.asList(new Object[][] {
//			{"a",1},
//			{"b",1},
//			{"c",1},
//			{"d",1}
//		});
//	}
	@Parameterized.Parameters
	public static Object[][] provideArgs() {
		return new Object[][] {
			{"a",1},
			{"b",1},
			{"c",1},
			{"d",1}
		};
	}
	@Test
	public void testGetTicketByHome() throws Exception {
		result = str+num;
	}
}

Suite测试

组合测试;关联运行一组测试。

@RunWith(Suite.class)
@SuiteClasses({ATest.class, BTest.class, CTest.class})
public class SuiteTest {}

Theories测试

JUnit4教程(五):Theory(理论)测试
理论测试,组合参数

  • @RunWith(Theories.class);标记类
  • @Theory;标记方法,带参
  • 方法参数
    @DataPoint; 标记单个变量
    @DataPoints; 标记静态方法、集合/数组变量
    @TestedOn; 标记方法参数,提供int数组
@RunWith(Theories.class)
public class TheoriesTest {
	@DataPoint
	public static Long l1 = 100L;
	@DataPoint
	public static Long l2 = 101L;
	
	@DataPoints
	public static int[] ints = {1, 2, 3, 4};
	
	@DataPoints
	public static String[] getStrs(){
		return new String[]{"a", "b", "c"};
	}
	
	@Theory
	public void test(Long l,int i, String str, @TestedOn(ints = { -1, -2 }) int j) {
		System.out.println(str+i+" "+l+", "+j);
	}
}

断言、假设

Assert静态方法,断言判定
Assume静态方法,false时跳过本次测试

规则

JUnit4教程(四):利用Rule扩展JUnit
扩展junit。
@Rule/@ClassRule;标记Rule类型public成员/静态变量。
常用Rule实现类:

TemporaryFolder创建临时文件
ErrorCollector错误收集,测试完后自动展示。catch收集。
Verifier自动结果校验。通过成员变量传递参数。
TestName获取测试方法名
ContiPerfRulecontiperf测试。

Jmeter

官网
易百教程
下载链接
JMeter基于协议,支持HTTP,JDBC,LDAP,SOAP,JMS和FTP等。

概念

测试计划
基本测试单元,由测试元素构成;至少包含一个线程组。
测试计划、测试元素均可单独保存;测试元素可合并到其他测试计划。
右上角感叹号,查看日志。
测试元素
https://jmeter.apache.org/usermanual/component_reference.html
thread group,线程组;
controllers,控制器;请求,顺序控制。两类:sampler采样器,logic controller逻辑控制器,控制sampler。
listeners,监听器;分析服务器返回结果,提供图表展示。
timers,计时器;模拟延时、暂停。
Configuration elements,配置元素;修改采样器发送的请求。
Pre/Post Processor,前/后处理;采样器前后处理。
http镜像
返回发送的请求;测试请求参数是否正常。
JDBC
导入jar包到./lib;jar包名格式:mysql-connector-java-X.X.X-bin.jar
注意数据库连接池名必填;异常时看日志。

问题

1,jmeter乱码,修改./bin/jmeter.properties,sampleresult.default.encoding=UTF-8。
2,ip、port等,在对应位置填写占位符${},引用参数。值就近优先。
3,jmeter请求乱码,修改路径后内容编码格式。

使用记录

JSON断言

Assert JSON Path exists填路径,$开头,点号连接;
勾选Addtionally assert value;
Expected value;填期望值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值