python: 基于unittest 的动态加载和自动化测试探讨 (二)
来源:互联网 发布:淘宝盗用品牌怎么处理 编辑:程序博客网 时间:2024/05/21 05:59
这是基于python 的 unittest 的动态加载和自动化测试探讨例子二。如果不使用python unittest,则可以参考我前的一篇文章
python: 动态加载和自动化测试探讨 (一)(http://blog.csdn.net/bigtree_3721/article/details/50599331)
###### 运行输出如下:
Running tests...
----------------------------------------------------------------------
test_abc (MyTestCase.myUT_case2) ... OK (0.016s)
test_calculate (MyTestCase.myUT_case2) ... OK (0.016s)
test_func1 (MyTestCase.myUT_case2) ... OK (0.016s)
----------------------------------------------------------------------
Ran 3 tests in 0.078s
OK
Generating XML reports...
OK
======================
Main.py 源代码如下:
'''
@author: hongbin'''
import xmlrunner
import importlib
import os
import unittest
from unittest.loader import defaultTestLoader
def main():
'''
Mini demo to show how SDM mini Jenkins worker do
1. load module dynamically
2. get one UT class
3. call unittest's method to collect all functions of this class
4. run these function alphabetically and generate reports
'''
#suite=unittest.TestSuite()
#suite.addTest(unittest.makeSuite(""))
#runner.run(suite)print("main() enters")
#module loaded dynamically
module=importlib.import_module("MyTestCase")
#call its function defined in this module
runner=xmlrunner.XMLTestRunner(verbosity=2,output='test-reports')
#根据给定的测试类,获取其中的所有测试方法,并返回一个测试套件
testClass = getattr(module, "myUT_case2")
testSuite=unittest.TestSuite()
testSuite=defaultTestLoader.loadTestsFromTestCase(testClass)
result = runner.run(testSuite)
if not result.wasSuccessful():
print("not successfully")
else:
print("OK")
if __name__== '__main__':
main()
MyTestCase.py 源代码如下
===============
import unittest
import xmlrunner
import os
import re
import time
from unittest.case import TestCase
def start():
print("myfunc() is called")
class myUT_case5(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_abs(self):
print("myUT_case5.test_abs is called")
#raise Exception("this case fail")
def test_price(self):
print("myUT_case5.test_price is called")
def test_quantity(self):
print("myUT_case5.test_quantity is called")
def _private(self):
print("myUT_case5._private is called")
class myUT_case1(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_name(self):
print("myUT_case1.test_name is called")
#raise Exception("this case fail")
def test_getlocation(self):
print("myUT_case1.test_getlocation is called")
def test_getID(self):
print("myUT_case1.test_getID is called")
def _private(self):
print("myUT_case1._private is called")
class myUT_case2(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_calculate(self):
print("myUT_case2.test_Calculate is called")
#raise Exception("this case fail")
def test_func1(self):
print("myUT_case2.test_func1 is called")
def test_abc(self):
print("myUT_case2.test_abc is called")
def _private(self):
print("myUT_case2._private is called")
- python: 基于unittest 的动态加载和自动化测试探讨 (二)
- python自动化测试unittest+selenium+HTMLTestRunner(二)
- python unittest 自动化测试
- python 动态加载 和 自动测试 探讨 (一)
- Python的unittest自动化测试框架
- 基于python+requests+unittest框架接口自动化测试设计开发
- 简单实现接口自动化测试(基于python+unittest)
- 基于python+requests+unittest框架接口自动化测试设计开发
- Python单元测试unittest加载方式之二:加载测试套件
- python自动化测试unittest+selenium+HTMLTestRunner(一)
- Python UnitTest 框架执行功能自动化测试
- Python + Requests + Unittest接口自动化测试
- python unittest做api自动化测试
- 初学者的Selenium自动化测试指南,基于Python(二)——自动化测试脚本小程序
- 【python单元测试unittest】加载测试套的代码修正
- appium2-基于python调用unittest框架对iOS进行自动化测试
- 基于python+requests+unittest框架接口自动化测试设计开发(简易型)
- Python-基于数据驱动模式的自动化测试框架搭建的的逐步实现(二)
- 【高斯消元】整数高斯消元模板
- C++还能重新辉煌吗?C++复杂性的思考
- android-Activity Intent.setFlags()与launchMode
- nyoj 954
- ArcGIS教程:“表面体积”的工作原理
- python: 基于unittest 的动态加载和自动化测试探讨 (二)
- 渲梦工厂(3dmax插件)下载 v2.0.8中文版
- 自学QT之实现程序启动画面
- Vim实战指南(二):光标移动技巧
- JQuery 中比较有趣的几个函数
- Eclipse常用技巧
- Python 各种运行错误(如:SyntaxError :invalid syntax)
- LINUX查看硬件命令
- 97. Interleaving String