unittest===unittest 的几种执行方式

#demo.py

import
requests import json class RunMain: def __init__(self, url, method, data=None): self.data = data self.url = url self.method = method self.res = self.run_main() def url_post(self): res = requests.post(url = self.url, data = self.data).json() return res def url_get(self): res = requests.get(url= self.url, data = self.data).json() return res # res 返回的json数据 # indent 缩进展示 # sort_keys 排序 # return json.dumps(res,indent = 2, sort_keys = True) #再封装一个方法,让它执行post和get请求 def run_main(self): res = None if self.method == 'GET': res = self.url_get() else: res = self.url_post() return res if __name__ == "__main__": url_p = "https://easy-mock.com/mock/5d0cce9536b12b2b2e62adc7/example/upload" data = { "hello":"world", "botoo":"123" } url_g ="https://easy-mock.com/mock/5d0cce9536b12b2b2e62adc7/example/query" g = RunMain(url_g,"GET").res print(g) p = RunMain(url_p,"POST",data = data ).res print(p)

 

 

 

#unittest-test.py

import unittest
from demo import RunMain
import HtmlTestRunner

class TestMethod(unittest.TestCase):
        #类方法

        @classmethod
        def setUpClass(cls):
                print("类执行之前的方法")
                
        @classmethod
        def tearDownClass(cls):
                print("类执行之后的方法")

                
        #每次方法之前执行
        def setUp(self):
                print("setup")

        def test_01(self):

                url_p = "https://easy-mock.com/mock/5d0cce9536b12b2b2e62adc7/example/upload"
                data = {
                        "hello":"world",
                        "botoo":"123"
                        }

                res = RunMain(url_p,"POST",data).res
                #print(res)
                self.assertIn("undefined_http",str(res['data']))
                globals()['img'] = res['data']['img']    #加入缓存

        def test_02(self):
                print(img)

                url_g ="https://easy-mock.com/mock/5d0cce9536b12b2b2e62adc7/example/query"
                res =  RunMain(url_g,"GET").res
                self.assertEqual("hah",res['data']['name'])
                #print(res)

        @unittest.skip
        def test_03(self):
                print(img)
                
        #每次方法之后执行
        def tearDown(self):
                print("teardown")


if __name__ == "__main__":
        """
        #方法一:
        unittest.main()
        """

        """
        #方法二:
        #加入容器中执行
        suite=unittest.TestSuite()
        suite.addTest(TestMethod("test_01"))
        suite.addTest(TestMethod("test_02"))
        suite.addTest(TestMethod("test_03"))
        unittest.TextTestRunner().run(suite)
        """

        """
        #方法三:
        #加入测试报告,执行所有case
        unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='example_dir'))
        """

        """
        #方法四:
        #加入测试报告,执行加入容器的case
        suite = unittest.TestSuite()
        suite.addTest(TestMethod("test_01"))
        suite.addTest(TestMethod("test_02"))
        suite.addTest(TestMethod("test_03"))
        testRunner=HtmlTestRunner.HTMLTestRunner(output='example_dir')
        testRunner.run(suite)
        """
        
                

【总结】

如果你也喜欢Python,这里有一群Python爱好者汇集在此。

关注微信公众号:【软件测试技术】,回复 888,获取QQ群号。

 

posted @ 2019-06-22 14:00  AAA五金批发王建军  阅读(1804)  评论(0编辑  收藏  举报