python数据驱动ddt的简单使用

1.以登录的测试用例为例子,先看一下之前的脚本:

特点:两个用例,写了两个函数,且测试数据直接写在函数内

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
# ConfigParser模块在python中用来读取配置文件
import configparser
import os
from common.my_test import MyTest
from common.get_token import get_token
import requests
import unittest
from common.testingedu_auth import testingedu_auth
from common.testingedu_logout import testingedu_logout
from common.get_log import get_log

log = get_log()

class TestingEduLogin(MyTest):

    # 读取接口配置文件
    config = configparser.ConfigParser()
    file_path = os.path.dirname(os.path.abspath('.')) + '/config/url_info.ini'
    config.read(file_path, encoding='UTF-8')

    def test_testingedu_login01(self):
        # log.info("用例名称:输入正确用户名和密码,验证登录是否成功")
        testingedu_auth()
        url = self.config.get('Login', 'LoginUrl')

        params = {
            "username": "XXX",
            "password": "123456"
        }

        headers = {"token": get_token()}

        response = requests.post(url=url, params=params, headers=headers)
        # print("响应体:", response.text)
        # print("状态码:", response.status_code)
        log.info("响应体:%s" % response.text)
        log.info("状态码: %s" % response.status_code)
        testingedu_logout()

    # 输入正确用户名和错误密码,验证登录是否成功
    def test_testingedu_login02(self):
        log.info("用例名称:输入正确用户名和错误密码,验证登录是否成功")
        # 重新获取token值
        testingedu_auth()
        url = self.config.get('Login', 'LoginUrl')
        params = {
            "username": "XXX",
            "password": "12345"
        }
        headers = {"token": get_token()}

        response = requests.post(url=url, params=params, headers=headers)
        log.info("响应体:%s" % response.text)
        log.info("状态码: %s" % response.status_code)
        testingedu_logout()


if __name__ == "__main__":
    # unittest.main()可直接将一个单元测试模块变成直接运行的测试脚本
    unittest.main()

运行结果:

2.用ddt优化后的脚本,需特别注意蓝色字体(用pip install ddt安装ddt模块):

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
# ConfigParser模块在python中用来读取配置文件
import configparser
import os
from common.my_test import MyTest
from common.get_token import get_token
import requests
import unittest
from common.testingedu_auth import testingedu_auth
from common.testingedu_logout import testingedu_logout
from common.get_log import get_log
import ddt

log = get_log()
# 测试数据
login_data = [
    {
        "username": "XXX",
        "password": "123456"
    },
    {
        "username": "XXX",
        "password": "12345"
    },
{
        "username": "YYY",
        "password": "123456"
    },
]
@ddt.ddt
class TestingEduLogin(MyTest):

    # 读取接口配置文件
    config = configparser.ConfigParser()
    file_path = os.path.dirname(os.path.abspath('.')) + '/config/url_info.ini'
    config.read(file_path, encoding='UTF-8')

    # 用例名称:登录
    @ddt.data(*login_data)
    def test_testingedu_login(self, value):
        testingedu_auth()
        url = self.config.get('Login', 'LoginUrl')
        headers = {"token": get_token()}

        response = requests.post(url=url, params=value, headers=headers)

        log.info("响应体:%s" % response.text)
        log.info("状态码: %s" % response.status_code)
        testingedu_logout()

if __name__ == "__main__":
    # unittest.main()可直接将一个单元测试模块变成直接运行的测试脚本
    unittest.main()

运行结果:

3.优化后的特点:

  1)只写一个函数即可

  2)测试数据与函数分离,方便日后维护

  3)测试数据中有几组数据(字典形式),则执行几次用例,详情请看运行结果

发布了26 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/changyixue/article/details/90667214