Pytest学习笔记7——接口自动化测试框架实战附源码(开荒篇)

  引言

  前面介绍了pytest框架一些基础知识与基本用法,掌握了一定基础后,我们可以进行项目实战,搭建pytest接口自动测试框架。

  框架设计思路

  画了一张草图,大家自行领会。

Pytest学习笔记7——接口自动化测试框架实战附源码(开荒篇)_第1张图片

 

   项目结构

Pytest学习笔记7——接口自动化测试框架实战附源码(开荒篇)_第2张图片

 

 

  源码展示

  将test_weatherapi.py测试用例源码附上,关键部分都有备注,知识点覆盖之前所讲的。

city = ['北京','上海','广州','深圳']
@allure.step('参数化请求数据: %s'% city)
def step_1():
    print("初始化数据")
    return city


# city = ['北京','上海','广州','深圳']
@pytest.fixture(params=city)
def city_name(request):
    print("城市名称: %s"%request.param)
    return request.param


@allure.epic('测试天气API接口'.center(30,'*'))
@allure.feature('测试模块')
@allure.suite('这是套件')
class TestWeather:
    """测试模块httpbin"""
    def setup(self):
        """所有用例执行前的条件"""
        self.conf = Config()
        self.data = Weather()
        self.test_assert = Assert.Assertions()
        self.request = Request.Request()
        self.results_list = Consts.RESULT_LIST
        self.logger = Log.MyLog()


    @allure.severity('normal')
    @allure.story('故事1:获取天气数据')
    @allure.title('获取单个城市的天气')
    @allure.description('获取深圳的天气')
    @allure.testcase('测试用例地址:www.***.com')
    @allure.issue('缺陷管理地址:https://www.zentao.net/')
    @allure.tag('这是tag')
    @pytest.mark.run(order=3)
    def test_001(self):
        """
        测试httpbin接口:get方法
        """
        # api:host
        host = self.conf.host_test
        req_url = 'https://' + host
        # api:url
        urls = self.data.url[0]
        # api:body
        params = self.data.data[0]
        # api:headers
        headers = self.data.header[0]
        print(host)
        print(req_url)
        print(urls)

        api_url = req_url + urls

        response = self.request.get_request(url=api_url,data=params,header=headers)
        self.test_assert.assert_code(response['code'],200)
        print(response['code'])
        self.results_list.append('True')
        print(self.results_list)
        print('接口返回数据SSSS: %s'%response)
        self.logger.info('接口返回数据: %s' % response)


    @allure.severity('normal')
    @allure.story('故事2:获取天气数据')
    @allure.title('获取不同城市的天气')
    @allure.description('获取北上广深城市的天气')
    @allure.testcase('测试用例地址:www.***.com')
    @allure.issue('缺陷管理地址:https://www.zentao.net/')
    @pytest.mark.parametrize('city_name',city,indirect=True)
    @pytest.mark.run(order=2)
    def test_002(self,city_name):
        # step_1()
        host = 'wthrcdn.etouch.cn'
        req_url = 'http://' + host
        urls = self.data.url[1]
        params = {'city':city_name}
        headers = self.data.header[1]

        api_url = req_url + urls

        response = self.request.get_request(url=api_url,data=params,header=headers)
        self.test_assert.assert_code(response['code'],200)
        print('接口返回数据: %s'%response)
        self.logger.info('接口返回数据: %s'%response)
        self.results_list.append('True')
        print(self.results_list)

  核心主程序代码

if __name__ == '__main__':
    conf = Configs.Config()
    log = Log.MyLog()
    log.info('初始化配置文件, path=' + conf.conf_path)

    shell = Shell.Shell()
  
    #exp: 异地启动报告
    cmd2 = 'pytest -s -q  --alluredir  allure_report --clean-alluredir'
    cmd3 = 'allure serve allure_report'
    
        try:
            os.chdir(r"./testcase")  # 跳到报告路径下
            print(os.getcwd())
            shell.invoke(cmd2)
            shell.invoke_2(cmd3)
            # log.info(shell.invoke_2(cmd3))
        except Exception:
            log.error('异地启动失败,请检查环境配置')
            raise
        else:
            log.info("没有执行")

  运行结果

[2020-06-07 23:04:11] [INFO] [INFO 2020-06-07 23:04:11]初始化配置文件, path=C:\Users\Administrator\Desktop\pytest_testAPI\config\config.ini

城市名称: 北京
接口返回数据: {'code': 200, 'body': {'data': {'yesterday': {'date': '6日星期六', 'high': '高温 28℃', 'fx': '东南风', 'low': '低温 18℃', 'fl': '', 'type': '晴'}, 'city': '北京', 'forecast': [{'date': '7日星期天', 'high': '高温 36℃', 'fengli': '', 'low': '低温 20℃', 'fengxiang': '南风', 'type': '多云'}, {'date': '8日星期一', 'high': '高温 38℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '西南风', 'type': '阴'}, {'date': '9日星期二', 'high': '高温 30℃', 'fengli': '', 'low': '低温 21℃', 'fengxiang': '东北风', 'type': '阴'}, {'date': '10日星期三', 'high': '高温 34℃', 'fengli': '', 'low': '低温 21℃', 'fengxiang': '东南风', 'type': '晴'}, {'date': '11日星期四', 'high': '高温 34℃', 'fengli': '', 'low': '低温 22℃', 'fengxiang': '东南风', 'type': '小雨'}], 'ganmao': '感冒易发期,外出请适当调整衣物,注意补充水分。', 'wendu': '31'}, 'status': 1000, 'desc': 'OK'}, 'text': '{"data":{"yesterday":{"date":"6日星期六","high":"高温 28℃","fx":"东南风","low":"低温 18℃","fl":"","type":"晴"},"city":"北京","forecast":[{"date":"7日星期天","high":"高温 36℃","fengli":"","low":"低温 20℃","fengxiang":"南风","type":"多云"},{"date":"8日星期一","high":"高温 38℃","fengli":"","low":"低温 26℃","fengxiang":"西南风","type":"阴"},{"date":"9日星期二","high":"高温 30℃","fengli":"","low":"低温 21℃","fengxiang":"东北风","type":"阴"},{"date":"10日星期三","high":"高温 34℃","fengli":"","low":"低温 21℃","fengxiang":"东南风","type":"晴"},{"date":"11日星期四","high":"高温 34℃","fengli":"","low":"低温 22℃","fengxiang":"东南风","type":"小雨"}],"ganmao":"感冒易发期,外出请适当调整衣物,注意补充水分。","wendu":"31"},"status":1000,"desc":"OK"}', 'time_consuming': 65.801, 'time_total': 0.065801}
['True']
.城市名称: 上海
接口返回数据: {'code': 200, 'body': {'data': {'yesterday': {'date': '6日星期六', 'high': '高温 27℃', 'fx': '东北风', 'low': '低温 21℃', 'fl': '', 'type': '多云'}, 'city': '上海', 'forecast': [{'date': '7日星期天', 'high': '高温 28℃', 'fengli': '', 'low': '低温 22℃', 'fengxiang': '东南风', 'type': '阴'}, {'date': '8日星期一', 'high': '高温 29℃', 'fengli': '', 'low': '低温 23℃', 'fengxiang': '东南风', 'type': '晴'}, {'date': '9日星期二', 'high': '高温 27℃', 'fengli': '', 'low': '低温 24℃', 'fengxiang': '东南风', 'type': '阴'}, {'date': '10日星期三', 'high': '高温 27℃', 'fengli': '', 'low': '低温 23℃', 'fengxiang': '东南风', 'type': '大雨'}, {'date': '11日星期四', 'high': '高温 29℃', 'fengli': '', 'low': '低温 24℃', 'fengxiang': '东风', 'type': '多云'}], 'ganmao': '感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。', 'wendu': '23'}, 'status': 1000, 'desc': 'OK'}, 'text': '{"data":{"yesterday":{"date":"6日星期六","high":"高温 27℃","fx":"东北风","low":"低温 21℃","fl":"","type":"多云"},"city":"上海","forecast":[{"date":"7日星期天","high":"高温 28℃","fengli":"","low":"低温 22℃","fengxiang":"东南风","type":"阴"},{"date":"8日星期一","high":"高温 29℃","fengli":"","low":"低温 23℃","fengxiang":"东南风","type":"晴"},{"date":"9日星期二","high":"高温 27℃","fengli":"","low":"低温 24℃","fengxiang":"东南风","type":"阴"},{"date":"10日星期三","high":"高温 27℃","fengli":"","low":"低温 23℃","fengxiang":"东南风","type":"大雨"},{"date":"11日星期四","high":"高温 29℃","fengli":"","low":"低温 24℃","fengxiang":"东风","type":"多云"}],"ganmao":"感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。","wendu":"23"},"status":1000,"desc":"OK"}', 'time_consuming': 16.963, 'time_total': 0.016963}
['True', 'True']
.城市名称: 广州
接口返回数据: {'code': 200, 'body': {'data': {'yesterday': {'date': '6日星期六', 'high': '高温 29℃', 'fx': '西南风', 'low': '低温 24℃', 'fl': '', 'type': '小雨'}, 'city': '广州', 'forecast': [{'date': '7日星期天', 'high': '高温 28℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '东南风', 'type': '大雨'}, {'date': '8日星期一', 'high': '高温 29℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '东南风', 'type': '阴'}, {'date': '9日星期二', 'high': '高温 31℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '南风', 'type': '大雨'}, {'date': '10日星期三', 'high': '高温 32℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '东南风', 'type': '小雨'}, {'date': '11日星期四', 'high': '高温 32℃', 'fengli': '', 'low': '低温 26℃', 'fengxiang': '南风', 'type': '小雨'}], 'ganmao': '感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。', 'wendu': '26'}, 'status': 1000, 'desc': 'OK'}, 'text': '{"data":{"yesterday":{"date":"6日星期六","high":"高温 29℃","fx":"西南风","low":"低温 24℃","fl":"","type":"小雨"},"city":"广州","forecast":[{"date":"7日星期天","high":"高温 28℃","fengli":"","low":"低温 26℃","fengxiang":"东南风","type":"大雨"},{"date":"8日星期一","high":"高温 29℃","fengli":"","low":"低温 26℃","fengxiang":"东南风","type":"阴"},{"date":"9日星期二","high":"高温 31℃","fengli":"","low":"低温 26℃","fengxiang":"南风","type":"大雨"},{"date":"10日星期三","high":"高温 32℃","fengli":"","low":"低温 26℃","fengxiang":"东南风","type":"小雨"},{"date":"11日星期四","high":"高温 32℃","fengli":"","low":"低温 26℃","fengxiang":"南风","type":"小雨"}],"ganmao":"感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。","wendu":"26"},"status":1000,"desc":"OK"}', 'time_consuming': 16.062, 'time_total': 0.016062}
['True', 'True', 'True']
.城市名称: 深圳
接口返回数据: {'code': 200, 'body': {'data': {'yesterday': {'date': '6日星期六', 'high': '高温 30℃', 'fx': '南风', 'low': '低温 26℃', 'fl': '', 'type': '中雨'}, 'city': '深圳', 'forecast': [{'date': '7日星期天', 'high': '高温 28℃', 'fengli': '', 'low': '低温 25℃', 'fengxiang': '南风', 'type': '中雨'}, {'date': '8日星期一', 'high': '高温 28℃', 'fengli': '', 'low': '低温 25℃', 'fengxiang': '东南风', 'type': '阴'}, {'date': '9日星期二', 'high': '高温 30℃', 'fengli': '', 'low': '低温 25℃', 'fengxiang': '东南风', 'type': '大雨'}, {'date': '10日星期三', 'high': '高温 31℃', 'fengli': '', 'low': '低温 28℃', 'fengxiang': '东南风', 'type': '小雨'}, {'date': '11日星期四', 'high': '高温 32℃', 'fengli': '', 'low': '低温 27℃', 'fengxiang': '东南风', 'type': '多云'}], 'ganmao': '感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。', 'wendu': '26'}, 'status': 1000, 'desc': 'OK'}, 'text': '{"data":{"yesterday":{"date":"6日星期六","high":"高温 30℃","fx":"南风","low":"低温 26℃","fl":"","type":"中雨"},"city":"深圳","forecast":[{"date":"7日星期天","high":"高温 28℃","fengli":"","low":"低温 25℃","fengxiang":"南风","type":"中雨"},{"date":"8日星期一","high":"高温 28℃","fengli":"","low":"低温 25℃","fengxiang":"东南风","type":"阴"},{"date":"9日星期二","high":"高温 30℃","fengli":"","low":"低温 25℃","fengxiang":"东南风","type":"大雨"},{"date":"10日星期三","high":"高温 31℃","fengli":"","low":"低温 28℃","fengxiang":"东南风","type":"小雨"},{"date":"11日星期四","high":"高温 32℃","fengli":"","low":"低温 27℃","fengxiang":"东南风","type":"多云"}],"ganmao":"感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。","wendu":"26"},"status":1000,"desc":"OK"}', 'time_consuming': 18.194, 'time_total': 0.018194}
['True', 'True', 'True', 'True']
.tianqiapi.com
https://tianqiapi.com
/api
200
['True', 'True', 'True', 'True', 'True']
接口返回数据SSSS: {'code': 200, 'body': {'cityid': '101280601', 'date': '2020-06-07', 'week': '星期日', 'update_time': '2020-06-07 22:45:12', 'city': '深圳', 'cityEn': 'shenzhen', 'country': '中国', 'countryEn': 'China', 'wea': '雨', 'wea_img': 'yu', 'tem': '26', 'tem1': '27', 'tem2': '25', 'win': '东南风 ', 'win_speed': '1级', 'win_meter': '小于12km/h', 'humidity': '96%', 'visibility': '17.6km', 'pressure': '999', 'air': '25', 'air_pm25': '25', 'air_level': '优', 'air_tips': '空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然!', 'alarm': {'alarm_type': '', 'alarm_level': '', 'alarm_content': ''}}, 'text': '{"cityid":"101280601","date":"2020-06-07","week":"\\u661f\\u671f\\u65e5","update_time":"2020-06-07 22:45:12","city":"\\u6df1\\u5733","cityEn":"shenzhen","country":"\\u4e2d\\u56fd","countryEn":"China","wea":"\\u96e8","wea_img":"yu","tem":"26","tem1":"27","tem2":"25","win":"\\u4e1c\\u5357\\u98ce ","win_speed":"1\\u7ea7","win_meter":"\\u5c0f\\u4e8e12km\\/h","humidity":"96%","visibility":"17.6km","pressure":"999","air":"25","air_pm25":"25","air_level":"\\u4f18","air_tips":"\\u7a7a\\u6c14\\u5f88\\u597d\\uff0c\\u53ef\\u4ee5\\u5916\\u51fa\\u6d3b\\u52a8\\uff0c\\u547c\\u5438\\u65b0\\u9c9c\\u7a7a\\u6c14\\uff0c\\u62e5\\u62b1\\u5927\\u81ea\\u7136\\uff01","alarm":{"alarm_type":"","alarm_level":"","alarm_content":""}}', 'time_consuming': 338.721, 'time_total': 1.338721}
.
5 passed in 1.79s

Generating report to temp directory...
Report successfully generated to C:\Users\ADMINI~1\AppData\Local\Temp\8814248789759504727\allure-report
Starting web server...
Server started at . Press  to exit

 

  测试报告

Pytest学习笔记7——接口自动化测试框架实战附源码(开荒篇)_第3张图片

 

你可能感兴趣的:(Pytest学习笔记7——接口自动化测试框架实战附源码(开荒篇))