文章目录

1、框架介绍

2、安装JDK8(包括配置环境变量)

3、安装Python3(包括配置环境变量)

4、安装scoop

5、安装windows版的allure

6、安装python版的selenium

7、安装谷歌浏览器

8、安装谷歌浏览器驱动

9、检查pytest是否已经安装(如果没有安装需要安装)

10、安装allure-pytest

11、安装pytest-ordering

12、安装requests框架

13、安装jsonpath框架

14、控制台输出测试结果

15、浏览测试报告

16、用例1的代码

17、用例2的代码

18、用例3的代码

19、运行测试集的代码

20、参考资料

1、框架介绍

框架

安装的版本

描述(作用)

selenium

python版

UI自动化框架,操纵各种主流浏览器

pytest

python版

测试框架,类似junit, testng, unittest

allure

windows版

生成html格式的测试报告

requests

python版

接口自动化框架,发送http请求

jsonpath

python版

json数据解析框架

2、安装JDK8(包括配置环境变量)

3、安装Python3(包括配置环境变量)

4、安装scoop

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

5、安装windows版的allure

scoop install allure

6、安装python版的selenium

pip install selenium

7、安装谷歌浏览器

8、安装谷歌浏览器驱动

1)下载chromedriver.exe

2)将该驱动文件放到python的安装目录里面

比如:D:\Python3\chromedriver.exe

9、检查pytest是否已经安装(如果没有安装需要安装)

10、安装allure-pytest

pip install allure-pytest

11、安装pytest-ordering

pip install pytest-ordering

12、安装requests框架

pip install requests

13、安装jsonpath框架

pip install jsonpath

14、控制台输出测试结果

15、浏览测试报告

16、用例1的代码

from selenium.webdriver import *

import pytest

import allure

from time import *

class TestSearch:

@pytest.fixture(scope="module", autouse=True)

def before(self):

global driver

driver = Chrome()

yield

driver.quit()

@allure.feature("模块名:网站首页")

@allure.story("功能名:全文检索")

@allure.testcase("用例名:使用明星作为关键字进行搜索")

@allure.step(title=u'搜索')

@pytest.mark.parametrize('url, input_box_id, search_btn_id, keyword, expected',

[('https://cn.bing.com', 'sb_form_q', 'sb_form_go', u'赵丽颖', u'冯绍峰'),

('https://www.sogou.com/', 'query', 'stb', u'冯绍峰', u'赵丽颖'),

],

ids=['测试必应网站的搜索功能', '测试搜狗网站的搜索功能']

)

def test_search(self, url, input_box_id, search_btn_id, keyword, expected):

"""

测试用例

:param url:

:param input_box_id:

:param search_btn_id:

:param keyword:

:param expected:

:return:

"""

global driver

# 最大化窗口

driver.maximize_window()

# 设置默认的等待时长

driver.implicitly_wait(15)

# 打开网页

driver.get(url)

# 点击搜索框

driver.find_element_by_id(input_box_id).click()

# 输入关键字

driver.find_element_by_id(input_box_id).send_keys(keyword)

sleep(3)

# 点击搜索按钮

driver.find_element_by_id(search_btn_id).click()

sleep(5)

assert expected in driver.page_source

17、用例2的代码

import pytest

import allure

import requests

import logging

import json

import jsonpath

class TestEmpSearchByKeywordFail:

@allure.feature("人事模块")

@allure.story("员工查询功能")

@allure.testcase("用例名:测试查询失败的情况")

@allure.step(title=u'搜索')

@pytest.mark.parametrize('host, path, params, expected',

[(

'https://tes.yangzc.cn', '/xlc-ops-b/entryUser/getUserPage',

{'page': 1, 'rows': 10, 'isAudit': 1, 'navigation': '', 'key': u'王小姐',

'organId': '6cda4ede186b4847a86886df6977acbf',

'gangweiId': '', 'schemeId': '', 'functionId': ''}, {'msg': '登录态失效'}

)

],

ids=['非登录状态,查询失败']

)

def test_search_fail(self, host, path, params, expected):

"""

根据关键字搜索员工

:param host:

:param path:

:param params:

:param expected:

:return:

"""

url = host + path

response = requests.post(url, params)

logging.info(response.text)

actual_dict = json.loads(response.text)

for key in expected:

real_values = jsonpath.jsonpath(actual_dict, '$..'+key)

assert expected[key] in real_values

18、用例3的代码

from common.utils import *

import pytest

import allure

import requests

import logging

import json

import jsonpath

class TestEmpSearchByKeywordSuccess:

@allure.feature("人事模块")

@allure.story("员工查询功能")

@allure.testcase("用例名:测试查询成功的情况")

@allure.step(title=u'搜索')

@pytest.mark.parametrize('host, path, params, expected',

[

(

'https://tes.yangzc.cn',

'/xlc-ops-b/entryUser/getUserPage',

{

'page': 1,

'rows': 10,

'isAudit': 1,

'navigation': '',

'key': u'王小姐',

'organId': '6cda4ede186b4847a86886df6977acbf',

'gangweiId': '',

'schemeId': '',

'functionId': '2'

},

{

'code': 0,

'userName': u'王小姐'

}

),

(

'https://tes.yangzc.cn',

'/xlc-ops-b/entryUser/getUserPage',

{

'page': 1,

'rows': 10,

'isAudit': 1,

'navigation': '',

'key': '',

'organId': '6cda4ede186b4847a86886df6977acbf',

'gangweiId': '',

'schemeId': '',

'functionId': '2'

},

{

'code': 0

}

)

],

ids=['关键字为姓名,查询成功', '关键字为空,查询成功']

)

def test_search_success(self, host, path, params, expected):

"""

根据关键字搜索员工

:param host:

:param path:

:param params:

:param expected:

:return:

"""

url = host + path

response = requests.post(url, params, cookies=cookies)

logging.info(response.text)

actual_dict = json.loads(response.text)

for key in expected:

real_values = jsonpath.jsonpath(actual_dict, '$..'+key)

assert expected[key] in real_values

@pytest.fixture(scope="module", autouse=True)

def before(self):

global cookies

cookies = login('https://tes.yangzc.cn/xlc-ops-b/login', 'yangzc', '123456',

'7a85fa40b20245f0bec3746593bd713f', True)

19、运行测试集的代码

from time import *

import os

import pytest

if __name__ == '__main__':

# 取当前时间

now = strftime("%Y-%m-%d-%H_%M_%S", localtime(time()))

pytest.main(['-s', '-q', 'cases/', '--alluredir', 'report-{0}'.format(now)])

os.system('allure generate report-{0}/ -o report-{0}/html'.format(now))

20、参考资料

[01] Python单元测试框架之pytest – 验证

[02] pytest参数化

[03] Selenium + Pytest + Allure UI测试过程

[04] pytest + allure的安装及使用

[05] Python + allure 报告

[06] pytest-调整测试用例的执行顺序

[07] 解决intellij idea控制台中文乱码(亲测有用)

[08] 开源自动化测试平台介绍一览

[09] Lego-美团接口自动化测试实践

[10] Allure–自动化测试报告生成

[11] 接口自动化测试框架开发 (pytest+allure+aiohttp+ 用例自动生成)

[12] swagger 自动生成接口测试用例

[13] 实现简单的接口自动化测试平台

[14] 自研的接口自动化测试平台

[15] 基于 HttpRunner 的 Web 测试平台

[16] PyTest运行指定的测试集

[17] python requests 自动管理 cookie 。 get后进行post发送数据---》最简单的刷票

[18] python接口自动化测试七:获取登录的Cookies,并关联到下一个请求

[19] requests发送post请求的一些疑点

[20] Python爬虫之requests库(三):发送表单数据和JSON数据

[21] Pytest高级进阶之Fixture

[22] pytest的一些高阶用法

[23] python中jsonpath模块的运用

[24] JsonPath从多层嵌套Json中解析所需要的值

[25] Pytest和Allure测试框架-超详细版+实战

[26] pytest+allure 报告标题问题

[27] pytest参数化、标记用例、生成html报告

[28] allure用例定制参数及报告效果展示

[29] python接口测试:自动保存cookies

微信扫一扫关注公众号

点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN

软件测试学习交流QQ群号:511619105

软件测试学习资料

《自动化测试教程》

Logo

一站式 AI 云服务平台

更多推荐