扩展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如何拆分字符串(基于各种分隔符),但不保留空格?   解析。Json格式的txt文件和knime中的java   java Spring rest api为什么在rest api调用的响应中更改了数据类型   升华文本3抛出java。lang.ClassNotFoundException,而记事本++不存在   java Android指纹扫描仪在尝试5次后停止工作?   java Android如何设置精确的重复报警?   java如何使用HTTPGET connect为access API输入用户名和密码   java当测试报告显示没有测试失败时,Gradle为什么说“有失败的测试”?   用Gson实现java获取响应   MapReduce程序中函数错误的java不可映射参数   java spring安全性不符合自动代理的条件   java GWT使用异步回调进行同步/阻塞调用   java奇怪的类数组问题无法在jsp中显示   如何在java中使用PrinterJob使用epl打印条形码   java如何在JTable中居中单元格   将Java Mockito测试转换为Kotlin   html Java正则表达式模式匹配到多个相同标记   testCompile中缺少java Gradle(Android)多项目依赖项   在输入提示后输入字符串时发生java FileNotFoundException