• 常见的python的unittest用法


      python的unittest好处是通过python脚本编写用例,每个用例可以单独调试初始化和清理动作,因为都是用例都是代码所以调试起来也很方便;它的缺点是得先学会python,难易程度见仁见智吧,对于我来说那些用excel来写测试用例的框架,我看到里面的用例头都大,还不如看代码来的简单呢。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os,sys
    import time
    import datetime
    import unittest
    
    class NginxTest(unittest.TestCase):
        def setUp(self): #如果不需要每个case都预置和清理环境,而是每个class用一次,只需要用setUpClass、tearDownClass代替即可,如果是整个文件只需要用一次,则用要用 setUpModule() 和 tearDownModule() 这两个函数了,注意是函数,与 TestCase 类同级
            #预置环境
            print '--------------NginxTestSetUp--------------\n'
        def tearDown(self):  
            #清理环境
            print '--------------NginxTestClear--------------\n'  
            
        def test_nginx(self):
            print 'test_nginx'        
       
        def test_nginxlog(self):               
            print 'test_nginxlog'
            
        @unittest.skip("must skipping") #必须跳过下面用例,相当少用
        def test_mustskip(self):
            print 'test_mustskip'
        
        # def test_1(self):
            # a=1
            # return 1
        @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")#根据条件跳过下面这个用例
        def test_maybeskip(self):
            print 'test_maybeskip'
        
        def suite_1(self):#非test开头的用例在NginxTest中不会被跑到
            print 'suite_1'
            
        def suite_2(self):
            print 'suite_2'         
        
    
    class PhpTest(unittest.TestCase):#因为每个接口的预置环境可能不一样,所以每个接口的用例应该都用单独class来包含,不过每个class的用例都还是要用test开头
        def setUp(self): 
            #预置环境
            print '--------------PhpTestSetUp--------------\n'
        def tearDown(self):  
            #清理环境
            print '--------------PhpTestClear--------------\n'  
            
        def test_php(self):
            print 'test_php'        
       
        def test_phplog(self):               
            print 'test_phplog'
        
        
    def suite():#这个表示测试集,不要放在class内,否则会提示"没有这样的测试方法在<class'myapp.tests.SessionTestCase'>:的runTest ",我觉得它唯一的好处就是调试的时候可以单独调试某个class而已,我一般不用它,调试时可以注释不需要的class啊 ;-)。不同接口用不同的class也是一种用法,不过那样用我下面说的import不同py的方法更好,因为所有用例写在一起的话文件太大了 ;-)。
        suite = unittest.TestSuite()
        suite.addTest(NginxTest("suite_1"))
        suite.addTest(NginxTest("suite_2"))
        suite.addTest(PhpTest("test_php"))
        suite.addTest(PhpTest("test_phplog"))
        unittest.TextTestRunner().run(suite)
        
    if __name__ == '__main__':
        # unittest.main(exit = False,verbosity=2)#它是全局方法,把它屏蔽后,不在suite的用例就不会跑,exit = False表示中间有用例失败也继续执行;还有比较常用的verbosity=2,表示显示def名字
        suite()#执行suite
    

      如果接口相当多,为了方便维护,建议每个case用独立的py来写,然后用一个“总入口”去import所有py,然后再调用就行了,这是suite就派上用场了

      举个例子,luatestcase.py如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os
    import time
    import datetime
    import unittest
    
    class LuaTest(unittest.TestCase):
        def runTest(self):
            print 'anything'
        def setUp(self): 
            #预置环境
            print '--------------LuaTestsetUp--------------\n'
        def tearDown(self):  
            #清理环境
            print '--------------LuaTestclear--------------\n'  
            
        def test_lua(self):
            print 'test_lua'        
       
        def test_lualog(self):               
            print 'test_lualog'
            
    def casesuite():
        suite = unittest.TestSuite()
        suite.addTest(LuaTest("test_lua"))
        suite.addTest(LuaTest("test_lualog"))
        unittest.TextTestRunner().run(suite)

      调用luatestcase.py的“总入口py”就得这样写:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os,sys
    import time
    import datetime
    import unittest
    
    import luatestcase
    
    if __name__ == '__main__':
        luatestcase.casesuite()

      如果luatestcase.py没有用casesuite“收集”它的用例的话,总入口调不了里面的用例的,不知道是不是我的用法有问题,如果有朋友知道欢迎指点。有的文章说用discover可以实现同样的需求,不过我不会。。。

      如果要保存unitest的测试输出日志,则需要用到TextTestRunner,例子如下

    log_file = "log_file.txt"
    f = open(log_file, "w")
    runner = unittest.TextTestRunner(stream=f,verbosity=2)
    unittest.main(exit = False,testRunner=runner)
    f.close()
    

      补充一个方法:每个接口的测试用例按照普通的unittest格式来写,放到统一的目录中,然后用一个总的py去遍历这些unittest文件,然后用os.popen打开,把这些用例的测试结果read下来保存到一个总的log文件中就行了。  

      最后说一下,我最常用的方法是assertEqual、assertNotEqual、assertTrue。

    作者:肥狐
    出处:http://idbeta.cnblogs.com/
    本博客内除了标题带[转]字样外的所有文章,均采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权,任何违反本协议的行为均属于非法行为。如需非商业性转载,必须保留此段声明,且在文章页面明显位置给出原文连接。如需商业性转载出版,请直接和我联系。
    如果您看了本篇博客,觉得对您有所收获,请点击右下方的【推荐】,同时欢迎您【关注我】
    Creative Commons License
  • 相关阅读:
    postgresql 53300错误
    linux su失败:无法设置用户ID:资源暂时不可用
    shell中使用带密码的方式直接pg_dump和psql
    pg数据库查询表大小
    linux安装postgresql简洁版
    检查linux版本命令
    博客园后台搜索自己的博客
    欧式洗车
    做生意
    无线AP隔离
  • 原文地址:https://www.cnblogs.com/idbeta/p/5040187.html
Copyright © 2020-2023  润新知