扩展unittest的测试框架,提供灵活的测试套件配置、并发执行、html/xunit报告和数据驱动实用程序。

unishark的Python项目详细描述


简介

unishark在 以下方式:

  • 使用字典配置(或类似于yaml/json的配置)自定义测试套件。
  • 在不同级别同时运行测试。
  • 以html/xunit格式生成完善的测试报告。
  • 提供数据驱动的decorator来加速测试的编写。

对于现有UNITSTEST,前三个特征可以用单个配置立即获得,而不改变任何测试代码。

测试配置

下面是一个yaml格式的配置示例(您也可以编写它 直接在dict()中:

suites:my_suite_name_1:package:my.package.namegroups:my_group_1:granularity:modulemodules:[test_module1,test_module2]except_classes:[test_module2.MyTestClass3]except_methods:[test_module1.MyTestClass1.test_1]my_group_2:granularity:classdisable:Falseclasses:[test_module3.MyTestClass5]except_methods:[test_module3.MyTestClass5.test_11]concurrency:level:modulemax_workers:2my_suite_name_2:package:my.package.namegroups:my_group_1:granularity:methodmethods:[test_module3.MyTestClass6.test_13,test_module3.MyTestClass7.test_15]concurrency:level:classmax_workers:2my_suite_name_3:package:another.package.namegroups:group_1:granularity:packagepattern:'(\w+\.){2}test\w*'except_modules:[module1,module2]except_classes:[module3.Class1,module3.Class3]except_methods:[module3.Class2.test_1,module4.Class2.test_5]concurrency:level:methodmax_workers:20reporters:html:class:unishark.HtmlReporterkwargs:dest:logsoverview_title:'ExampleReport'overview_description:'Thisisanexamplereport'xunit:class:unishark.XUnitReporterkwargs:summary_title:'ExampleReport'test:suites:[my_suite_name_1,my_suite_name_2,my_suite_name_3]concurrency:type:processesmax_workers:3reporters:[html,xunit]name_pattern:'^test\w*'

它使用排除的一些测试用例配置3个测试套件,同时运行定义的测试集,并在测试结束时生成html和xunit(默认junit)格式的报告。

注意:在0.2.x版本中,“max_workers”直接设置在“test”下,“max_workers”和“concurrency_level”直接设置在“{suite name}”下。

要运行它,只需添加以下代码:

importunisharkimportyamlif__name__=='__main__':withopen('your_yaml_config_file','r')asf:dict_conf=yaml.load(f.read())# use a 3rd party yaml parser, e.g., PyYAMLprogram=unishark.DefaultTestProgram(dict_conf)unishark.main(program)

可以找到一个html报告示例Here

数据驱动

下面是使用@unishark.data驱动的一些效果。

“json”式数据驱动:

@unishark.data_driven(*[{'userid':1,'passwd':'abc'},{'userid':2,'passwd':'def'}])deftest_data_driven(self,**param):print('userid: %d, passwd: %s'%(param['userid'],param['passwd']))

结果:

userid: 1, passwd: abc
userid: 2, passwd: def

“args”式数据驱动:

@unishark.data_driven(userid=[1,2,3,4],passwd=['a','b','c','d'])deftest_data_driven(self,**param):print('userid: %d, passwd: %s'%(param['userid'],param['passwd']))

结果:

userid: 1, passwd: a
userid: 2, passwd: b
userid: 3, passwd: c
userid: 4, passwd: d

交叉乘法数据驱动:

@unishark.data_driven(left=list(range(10)))@unishark.data_driven(right=list(range(10)))deftest_data_driven(self,**param):l=param['left']r=param['right']print('%d x %d = %d'%(l,r,l*r))

结果:

0 x 1 = 0
0 x 2 = 0
...
1 x 0 = 0
1 x 1 = 1
1 x 2 = 2
...
...
9 x 8 = 72
9 x 9 = 81

你可以得到参数值的排列(重复)。 通过执行:

@unishark.data_driven(...)@unishark.data_driven(...)@unishark.data_driven(...)...

“json样式”中的多线程数据驱动:

@unishark.multi_threading_data_driven(2,*[{'userid':1,'passwd':'abc'},{'userid':2,'passwd':'def'}])deftest_data_driven(self,**param):print('userid: %d, passwd: %s'%(param['userid'],param['passwd']))

结果:与使用unishark.data驱动的结果相同,但最多生成2个线程,每个线程使用一组输入(userid、passwd)运行测试。

“args样式”中的多线程数据驱动:

@unishark.multi_threading_data_driven(5,time=[1,1,1,1,1,1,1,1,1,1])deftest_data_driven(self,**param):sleep(param['time'])

结果:生成了5个线程来同时运行10组输入的测试(每个线程仅休眠1秒)。 运行总共需要大约2秒(如果使用Unishark.Data_-driven,则需要10秒)。

有关详细信息,请访问Project_Home并阅读readme.md。

更改日志

0.3.2(2015-11-24)

  • added multiprocessing suites (which can bypass CPython’s GIL and utilize multi-cores).
  • modified result, runner and reporter classes to be picklable for multiprocessing.
  • supported running with Jython.

0.3.1(2015-11-12)

  • fixed the issue of still running test methods even when setUpClass/setUpModule raises exception in concurrency mode.
  • fixed error descriptions of class or module level fixtures when they raise exceptions.

0.3.0(2015-11-06)

  • rewrote concurrent execution model. Now test fixtures setUpModule/tearDownModule setUpClass/tearDownClass will be executed once and only once no matter what concurrency level(module/class/method) of a suite is. Fixed the problem that module fixtures were executed multiple times when concurrency level was ‘class’ or ‘method’, and class fixtures were executed multiple times when concurrency level was ‘method’.
  • changed the format of the concurrency-related settings in the dict config. Now ‘max_workers’ and ‘level’ are keys in the ‘concurrency’ sub-dict.
  • moved BufferedTestResult class from the runner module to the new result module which makes more sense.

0.2.3(2015-10-01)

  • enabled ‘module’ and ‘method’ level concurrent execution in a suite.

0.2.2(2015-08-12)

  • support loading tests from a package with pattern matching, and excluding modules/classes/methods from the loaded tests.
  • add load_tests_from_package and load_tests_from_modules api.
  • rename load_test_from_dict to load_tests_from_dict.
  • fix that verbose stdout mode does not print test method doc string.
  • fix that tests loaded with method granularity are not filtered by method name pattern.
  • less strict dependency versions.

0.2.1(2015-05-11)

  • support data-driven with multi-threads.

0.2.0(2015-04-04)

  • support running tests in parallel.
  • support configuring test suites, test reporters and concurrent tests in a single dict/yaml config.
  • improve HtmlReporter and XUnitReporter classes to be thread-safe.
  • allow user to generate reports with their own report templates.
  • allow user to filter loaded test cases by setting method name prefix in the test config.
  • bugs fix.
  • improve documentation.

0.1.2(2015-03-25)

  • hotfix for setup.py (broken auto-downloading dependencies)
  • bugs fix.

0.1.1(2015-03-24)

  • support loading customized test suites.
  • support a thread-safe string io buffer to buffer logging stream during the test running.
  • support writing logs, exceptions into generated HTML/XUnit reports.
  • offer a data-driven decorator.
  • initial setup (documentation, setup.py, travis CI, coveralls, etc.).

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Android如何使用登录名和密码正确查询在线数据库?   java如何在运行时更改Jasper Reports PDF版本   javascript找到合适的Java WebSocket框架生成特定的JS代码   macos Java系统。getProperty问题   java ARCore TransformableNode在拖动后将localPosition设置为[x=0.0,y=0.0,z=0.0]   java覆盖无处不在?   java使用GET调用将登录表单连接到swing中的url   java如何拆分字符串并获取最后一部分?   java无法在firebase数据库的文本查看器上设置文本   java如何使用@RabbitListener优雅地停止使用消息   java向使用paintComponent方法绘制的组件添加鼠标侦听器   java是一个高效的大型单词数组   广播接收器包(?)中的java startActivity()   在Java中是否有JPanel限制?   java定位按钮不起作用   java jsp编译错误   java如何从Firebase检索多个数据项并将这些字符串合并在一起?   用于检测重复符号的java正则表达式