一、JUnit请求
JUnit是一个java单元测试框架。在讲Jmeter中Junit请求的之前,不得不先看下基本的JUnit使用.
1.1 编写一个测试文件及被测文件
study.java
package com.ittest; public class study { public int add(int x,int y){ return x +y; } public int divide(int x,int y){ return x/y; } public static void main(String[] args) { int z = new study().add(3,5); System.out.println(z); } }
studyTest.java
package test.com.ittest; import com.ittest.study; import org.junit.*; import static junit.framework.TestCase.assertEquals; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; /** * study Tester. * * @author <kenwu> * @since <pre>八月 29, 2018</pre> * @version 1.0 */ public class studyTest { @BeforeClass public static void beforeclass() throws Exception{ System.out.println("before class.."); } @Before public void before() throws Exception { System.out.println("befor"); } /** * * Method: add(int x, int y) * */ @Test public void testAdd() throws Exception { //TODO: Test goes here... int z = new study().add(2,4); // assertEquals(6, z); assertThat(z, is(8)); } /** * * Method: divide(int x, int y) * */ @Test public void testDivide() throws Exception { //TODO: Test goes here... int z = new study().divide(8,2); System.out.println(z); } /** * * Method: main(String[] args) * */ @After public void after() throws Exception { System.out.println("after"); } @AfterClass public static void afterclass() throws Exception{ System.out.println("after class.."); } }
简单说一下:
@Before:每一个测试方法之前运行 @After:每一个测试方法之后运行 @BeforeClass:所有测试开始之前运行,静态方法 @AfterClass:所有测试结束之后运行,静态方法 @Test:测试方法 @Test(expected=java.lang.ArithmeticException.class,timeout=100)a是在测试出现异常的情况下告知我们出现的异常信息,类似与try-catch中的e.printstacktrace() 方法,比较简单。 b中的timeout=100,运行时间限制在100ms以内(通常在测试代码运行效率时这样设置)
1.2 打包生成jar包
1.3 将打包的jar放入指定位置
将该Jar包导入到.\Jmeter\apache-jmeter-4.0\lib\junit目录下。(如果编写代码时,存在第三方Jar包引入,那么就将该第三方Jar包放在.\Jmeter\apache-jmeter-4.0\lib中)
1.4 创建JMeter-Junit Request
重新启动JMeter,添加两条Junit Request,查看结果树