美文网首页自动化测试
接口测试--discover读取所有测试用例

接口测试--discover读取所有测试用例

作者: 胆小的米老鼠 | 来源:发表于2018-09-30 11:14 被阅读39次

运行测试用例脚本:

import unittest
import  requests
import  time
class NationLand(unittest.TestCase):
    '''换成你公司的项目介绍'''

    def setUp(self):
        self.url = "换成你公司的接口地址"

    def testshowList(self):
        '''current 默认为1,代码页码,size 默认为 10'''
        land.setUp()
        form ={
          "CODE": "",
          "LANDBATCH": "",
          "current": "2",
          "size": "10"
        }
        r = requests.post(self.url,data=form)
        code = r.status_code
        self.assertEqual(200,code)
        print("测试通过")

    def testcurrenIsnull(self):
        '''current 为空,size为正常10'''
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "",
            "size": "10"
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400,code)
        print("测试通过")

    def testsizeIsnull(self):
        '''current 为空,seze为正常10'''
        land.setUp()
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "1",
            "size": ""
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400, code)
        print("测试通过")

    def testcursizeIsnull(self):
        land.setUp()
        '''current 为null,seze为null'''
        land.setUp()
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "",
            "size": ""
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400, code)
        print("测试通过")


    def tearDown(self):
        pass


if __name__ == '__main__':

    test_dir = r"脚本保存路径"
    testlist = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py", top_level_dir=None)

    runndr = unittest.TextTestRunner()
    runndr.run(testlist)

生成测试报告

测试脚本修改:
import unittest
import  requests
import HTMLTestRunner
import  time,os
class NationLand(unittest.TestCase):
    '''这是用地批次管理接口测试'''

    def setUp(self):
        self.url = "http://192.168.0.197:8088/land/showList"

    def testshowList(self):
        '''current 默认为1,代码页码,size 默认为 10'''
        # self.url = "http://192.168.0.197:8088/land/showList"
        form ={
          "CODE": "",
          "LANDBATCH": "",
          "current": "2",
          "size": "10"
        }
        r = requests.post(self.url,data=form)
        code = r.status_code
        self.assertEqual(200,code)
        print("测试通过")

    def testcurrenIsnull(self):
        '''current 为空,size为正常10'''
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "",
            "size": "10"
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400,code)
        print("测试通过")

    def testsizeIsnull(self):
        '''current 为空,seze为正常10'''
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "1",
            "size": ""
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400, code)
        print("测试通过")

    def testcursizeIsnull(self):
        '''current 为null,seze为null'''
        form = {
            "CODE": "",
            "LANDBATCH": "",
            "current": "",
            "size": ""
        }
        r = requests.post(self.url, data=form)
        code = r.status_code
        self.assertEqual(400, code)
        print("测试通过")


    def tearDown(self):
        pass

生成测试报告:

import  testloadtest
import unittest
import HTMLTestRunner,os
def createsuit():
    '''创建测试方法'''
    test_unit= unittest.TestSuite()
    test_dir = r"脚本保存路径"
    testlist = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py", top_level_dir=None)
    for test_suit in testlist:
        for testcase in test_suit:
            test_unit.addTest(testcase)
    return  test_unit

with open("result.html","wb") as file:
    '''生成测试报告'''
    runner = HTMLTestRunner.HTMLTestRunner(stream=file,title="测试报告",description="执行用例")
    runner.run(createsuit())

测试报告效果图:

image.png

相关文章

网友评论

    本文标题:接口测试--discover读取所有测试用例

    本文链接:https://www.haomeiwen.com/subject/ttpkoftx.html