python: 基于unittest 的动态加载和自动化测试探讨 (二)

这是基于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")


        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值