» 您尚未 登录   注册 | 社区服务 | FTP中心 | 帮助 | 社区 | 无图版 | 测试百科  | 测试Blog 
软件测试基地论坛 -> 英语天天看 -> Junit Sampler Tutorial
 XML   RSS 2.0   WAP 

--> 本页主题: Junit Sampler Tutorial 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1687 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

Junit Sampler Tutorial


图片:

This tutorial attempts to explain the basic design, functionality and usage of the new Junit
Sampler for Jmeter. The sampler was introduced in 2.1.2 release of Jmeter. Earlier releases
do not have the sampler.
Design
The current implementation supports standard Junit convention and extensions, like
oneTimeSetUp and oneTimeTearDown. Other features can be added on request. The
sampler works like the JavaSampler with some differences.
1. rather than use Jmeter's test interface, it scans the jar files for classes extending junit's
TestCase class. This means any class or subclass.
2. Junit test jar files are copied to jmeter/lib/junit instead of jmeter/lib
3. Junit sampler does not use name/value pairs for configuration. The sampler assumes
setUp and tearDown will configure the test correctly.
4. The sampler measures the elapsed time only for the test method and does not include
setUp and tearDown.
5. Each time the test method is called, Jmeter will pass the result to the listeners.
6. Support for oneTimeSetUp and oneTimeTearDown is done as a method. Since Jmeter is
multi-threaded, we cannot call oneTimeSetUp/oneTimeTearDown the same way maven
does it.
7. The sampler reports unexpected exceptions as errors.
Functionality
Here is a description of the functionalty.
Name – name for the sample. This is the same as all jmeter samplers.
Package Filter – provides a way to filter the classes by package name.
Classname – the name of the class to test. The sampler will scan the jar files in
jmeter/lib/ext and jmeter/lib/junit for classes extending junit's TestCase.
Constructor String – a string to pass to the string constructor of the test class.
Test Method – the name of the method to test in the sampler.
Success message – a descriptive message indicating what success means.
Success code – an unique code indicating the test was successful.
Failure message – a descriptive message indicating what failure means.
Failure code – an unique code indicating the test failed
Error message – a description for errors
Error code – some code for errors. Does not need to be unique
Do not call setUp and tearDown – set the sampler not to call setUp and tearDown. By
default, setUp and tearDown should be called. Not calling those methods could affect the
test and make it inaccurate. This option should be used with caution. If the selected method
is oneTimeSetUp or oneTimeTearDown, this option should be checked.
Append assertion error – By default, the sampler will not append the assert failures to the
failure message. To see the message in the result tree, check the option.
Append runtime exception – By default, the sampler will not append the exceptions to
the failure message. To see the stacktrace, check the option
The current implementation of the sampler will try to create an instance using the string
constructor first. If the test class does not declare a string constructor, the sampler will look
for an empty constructor. Example below:
Empty Constructor:
public class myTestCase {
public myTestCase() {}
}
String Constructor:
public class myTestCase {
public myTestCase(String text) {
super(text);
}
}
By default, Jmeter will provide some default values for the success/failure code and message.
Users should define a set of unique success and failure codes and use them uniformly across
all tests.
Usage
Here is a short step-by-step.
1. write your junit test and jar the classes
2. copy and paste the jar files into jmeter/lib/junit directory
3. start jmeter
4. select “test plan”
5. right click add -> thread group
6. select “thread group”
7. right click add -> sampler -> junit request
8. enter “my unit test” in the name
9. enter the package of your junit test
10.select the class you want to test
11.select a method to test
12.enter “test successful” in success message
13.enter “1000” in success code
14.enter “test failed” in failure message
15.enter “0001” in failure code
16.select the thread group
17.right click add -> listener ->view results tree
One benefit of the Junit sampler is it allows the user to select any method from a variety of
unit tests to create a test plan. This should reduce the amount of code an user needs to write
to create a variety of test scenarios. From a basic set of test methods, different sequences
and tests can be created using Jmeter's GUI.
For example:
Test Plan1
• TestCase1.testImportCustomer
• TestCase2.testUpdateRandomCustomer
• TestCase1.testSelect100
• TestCase2.testUpdateOrder
• TestCase1.testSelect1000
TestPlan2
• TestCase1.testImportCustomer
• TestCase1.testSelect100
• TestCase1.testSelect1000
• TestCase2.testAdd100Customers
General Guidelines
Here are some general guidelines for writing Junit tests so they work well with Jmeter. Since
Jmeter runs multi-threaded, it is important to keep certain things in mind.
1. Write the setUp and tearDown methods so they are thread safe. This generally means
avoid using static memebers.
2. Make the test methods discrete units of work and not long sequences of actions. By
keeping the test method to a descrete operation, it makes it easier to combine test
methods to create new test plans.
3. Avoid making test methods depend on each other. Since Jmeter allows arbitrary
sequencing of test methods, the runtime behavior is different than the default Junit
behavior.
4. If a test method is configurable, be careful about where the properties are stored. Reading
the properties from the Jar file is recommended.
5. Each sampler creates an instance of the test class, so write your test so the setup happens
in oneTimeSetUp and oneTimeTearDown.
6. If you select a class and no methods show up, it means the sampler had a problem
creating an instance of the test class. The best way to debug this is to add some
System.out to your class constructor and see what is happening.


可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[楼 主] | Posted: 2006-06-27 22:09 顶端
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1687 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



来源:BonoyQa(Bonoy.com)

概述
本指南将阐述Jmeter的新的JUnit取样器的基本设计、功能和用法。取样器目前在Jmeter的CVS中,并不是Jmeter2.1发布的一部分。它将包含在下一个Jmeter发布中。

设计
当前的实现支持标准的JUnit规则和扩展,例如oneTimeSetUp和oneTimeTearDown。其他特性可以根据需要添加。它基本上如Java取样器一样工作,有些区别:

1. 它扫描jar文件来获得继承了junit的TestCase的类,而不是使用Jmeter的测试接口。这意味着任何类和子类。
2. Junit测试jar文件被复制到jmeter/lib/junit目录,而不是jmeter/lib目录。
3. Junit取样器不使用名称/值对来配置。它假定setUp和tearDown会正确配置测试。
4. 它仅测量测试方法花费的时间,而不包括setUp和tearDown。
5. 每次测试方法被调用,Jmeter会传递结果给监听器(listener)。
6. 对oneTimeSetUp和oneTimeTearDown的支持是将他们作为一个方法(method)。由于Jmeter是多线程的,我们不能像maven那样调用oneTimeSetUp/oneTimeTearDown。
7. 它意外的(unexpected)异常(Exception)报告为错误(errors)。


功能
下面是关于功能的描述:
名字(Name) - 样本名称。这与所有的jmeter取样器一样。
包过滤器(Package Filter)- 支持以包名来过滤类的方式。
类名(Classname)- 要测试的类的名称。它将扫描jmeter/lib/ext和jmeter/lib/junit中的jar文件来取得继承了junit的TestCase的类。
构造字符串(Constructor String) – 传递给测试类构造方法的字符串。
测试方法(Test Method)- 在取样器中要测试的方法的名称。
成功信息(Success Message)- 用于指示成功是什么意思的描述信息。
成功代码(Success Code)- 一个用于说明测试是成功的唯一代码。
失败信息(Failure Message) - 用于指示失败是什么意思的描述信息。
失败代码(Failure Code) - 一个用于说明测试失败的唯一代码。
错误信息(Error Message) - 关于错误的描述。
错误代码(Error Code) - 错误的代码。不需要唯一。
不要调用setUp和tearDown – 设置取样器不要调用setUp和tearDown。默认情况下,setUp和tearDown应该被调用。不调用那两个方法会影响测试,并使测试不准确。此选项应该小心使用。如果被选择的方法是oneTimeSetUp和oneTimeTearDown,这个选项要被选中。

Junit取样器当前的实现会首先尝试使用字符串构造方法创建实例。如果测试类没有声明一个字符串构造方法,它会查找一个空构造方法。事例如下:

空构造方法:
引用:
public class myTestCase{
public myTestCase(){
}
}


字符串构造方法:
引用:
public class myTestCase{
public myTestCase(String text){
super(text);
}
}

JUnit取样器配置:

默认情况下,Jmeter提供了缺省的成功/失败代码和信息。用户应该定义一组唯一的成功和失败代码并在所有的测试中一致地使用他们。

用法
下面是一个分布介绍如何使用Junit取样器:
1. 编写你的junit测试并将它打包成jar文件。
2. 复制并粘贴jar文件到jmeter/lib/junit目录中。
3. 启动jmeter。
4. 选择“测试计划”。
5. 右键点击“添加->线程组”。
6. 选择“线程组”。
7. 右键点击“添加->取样器->junit请求”。
8. 输入“我的单元测试”。
9. 输入你的junit测试包名。
10.选择你要测试的类。
11.选择要测试的方法。
12. 在成功信息中输入“测试成功”。
13.在成功代码中输入“1000”。
14.在失败信息中输入“测试失败”。
15.在失败代码中输入“0001”。
16.选择线程组。
17.右键单击“添加->监听器->察看结果树”。

Junit取样器的一个优势就是它允许用户在多个单元测试中选择任何方法来创建测试计划。它能够减少用户来创建各种各样的测试场景(scenario)的代码量。从一组基本的测试方法,使用Jmeter的图形用户界面能够创建不同的序列(sequence)和测试

示例

Test Plan1
• TestCase1.testImportCustomer
• TestCase2.testUpdateRandomCustomer
• TestCase1.testSelect100
• TestCase2.testUpdateOrder
• TestCase1.testSelect1000
TestPlan2
• TestCase1.testImportCustomer
• TestCase1.testSelect100
• TestCase1.testSelect1000
• TestCase2.testAdd100Customers

常用规则
这是一些常用的编写Junit测试的规则,根据这些规则编写的测试能够更好的在Jmeter中工作。由于Jmeter是多线程运行的,所以某些规则是很重要的。

1. 编写setUp和tearDown方法,确保他们是线程安全的。这一般意味着不要使用静态成员变量。
2. 使测试方法是离散的工作单元,而不是长的动作序列。通过维护测试方法为离散的操作,有利于组建测试方法为新的测试计划。
3. 避免使测试方法互相依赖。由于Jmeter允许测试方法以任何序列组织在一起,运行时行为与默认的Junit行为不一致。
4. 如果一个测试方法是可配置的,小心防止属性(Properties)文件的存储位置。建议从jar文件读取属性文件。
5. 每个取样器创建一个测试类的实例,因此编写你的测试使配置(setup)发生在oneTimeSetUp和oneTimeTearDown中。



可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[1 楼] | Posted: 2006-06-27 22:11 顶端
头昏昏




该用户目前不在线
级别: 测试新手
精华: 0
发帖: 1
基地声望: 1 点
基地币: 6574 Bug
基地贡献: 0 点
好评度: 0 点
在线时间:0(小时)
注册时间:2006-04-28
最后登录:2006-07-04
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

Junit 和 gunit 有什么区别啊

我现在 在做一个网站的单元测试 用哪个工具啊

谢谢

[2 楼] | Posted: 2006-07-04 20:42 顶端
lovefly_zero


该用户目前不在线
级别: 测试新手
精华: 0
发帖: 37
基地声望: 3 点
基地币: 6461 Bug
基地贡献: 0 点
好评度: 0 点
在线时间:2(小时)
注册时间:2006-06-29
最后登录:2006-11-21
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



没用过...
[3 楼] | Posted: 2006-07-05 08:19 顶端
snowgeng




该用户目前不在线
级别: 测试新手
精华: 0
发帖: 18
基地声望: 3 点
基地币: 6423 Bug
基地贡献: 0 点
好评度: 0 点
在线时间:2(小时)
注册时间:2006-07-16
最后登录:2006-07-26
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



I've never tried it. Unit test is usually done by programmer.
[4 楼] | Posted: 2006-07-16 15:08 顶端

软件测试基地论坛 -> 英语天天看




软件测试基地上海测仕信息技术有限公司旗下网站
Copyright © 2005-2007 Cntesting.com, All Rights Reserved
沪ICP备06057721号

Powered by PHPWind Code © 2003-06 PHPWind
Total 0.141802(s) query 5, Time now is:12-02 11:41, Gzip disabled
You can contact us


每日一句:Loading...