使用pytest與allure實(shí)現(xiàn)一個(gè)簡單的測試demo
python的一個(gè)測試框架pytest
python鄙視鏈:pytest 鄙視 > unittest 鄙視 > robotframework 鄙視 > 記流水賬 鄙視 > “hello world”小白!
由于在家里沒有什么業(yè)務(wù)相關(guān)的東西可以做鳍贾,所以將目光移向了我們的老朋友“豆瓣”
首先,實(shí)現(xiàn)一個(gè)簡單的頁面訪問并查看是否訪問正常
創(chuàng)建一個(gè)包含3個(gè)url的測試數(shù)據(jù)test_data
TEST_URL = 'https://www.douban.com'
TEST_BOOK_URL = 'https://book.douban.com'
TEST_MOVIE_URL = 'https://movie.douban.com'
test_data = [TEST_URL,TEST_BOOK_URL,TEST_MOVIE_URL]
接下來導(dǎo)入這次的主角們
import pytest
import allure
由于打算使用瀏覽器進(jìn)行測試順便導(dǎo)入一下selenium
from selenium import webdriver
開始測試環(huán)境的準(zhǔn)備
- 使用裝飾器
@pytest.fixture
將被裝飾的函數(shù)driver
可以當(dāng)作參數(shù)傳入測試代碼中 -
scope="module"
表示這個(gè)函數(shù)在module(該py文件)層面只會(huì)運(yùn)行一次 - 函數(shù)主體部分前三句是slenium的基本操作
- 使用
yield
生成器來返回driver
的作用是當(dāng)測試全部執(zhí)行完之后才會(huì)執(zhí)行后面的關(guān)閉瀏覽器的操作
@pytest.fixture(scope='module')
def driver():
driver = webdriver.Chrome(executable_path='./driver/chromedriver')
driver.maximize_window()
driver.implicitly_wait(3)
yield driver
driver.close()
- 前面兩行裝飾器的作用可以在報(bào)告中看到
-
@pytest.mark.parametrize
將待測試test_data
傳入,ids是測試點(diǎn)的名稱 - 函數(shù)
test_douban_url
的參數(shù)中傳入了上一步定義的url
和在fixture
中創(chuàng)建的driver
- 使用
assert
進(jìn)行斷言判斷結(jié)果的正確性
@allure.feature('豆瓣簡單測試')
@allure.story('進(jìn)入頁面')
@pytest.mark.parametrize('url',test_data,ids=['douban','book','movie'])
def test_douban_url(url,driver):
with allure.step('測試進(jìn)入:{}'.format(url)):
driver.get(url)
if url == TEST_URL:
assert driver.title == '豆瓣'
elif url == TEST_BOOK_URL:
assert driver.title == '豆瓣讀書'
else:
assert 0 #豆瓣電影故意報(bào)錯(cuò)
同理浅碾,增加一個(gè)搜索圖書的測試點(diǎn)
先寫一個(gè)搜索的函數(shù)
@allure.step('搜索:{1}')
def search(driver,message):
driver.find_element_by_id('inp-query').send_keys(message)
driver.find_element_by_xpath('//*[@id="db-nav-book"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()
return driver.title
再用fixture
創(chuàng)建一個(gè)function級(jí)別的函數(shù)(每次執(zhí)行都有環(huán)境準(zhǔn)備和清理的操作),跳轉(zhuǎn)到豆瓣讀書的首頁
@pytest.fixture
def book_driver(driver):
driver.get(TEST_BOOK_URL)
yield driver
driver.get(TEST_BOOK_URL)
測試函數(shù):
test_book_data = ['掃地出門','朋友之間','觀山海','魚翅與花椒']
@allure.feature('豆瓣簡單測試')
@allure.story('豆瓣讀書搜索')
@pytest.mark.parametrize('book',test_book_data)
def test_doubanbook(book,book_driver):
title_name = search(book_driver,book)
assert book in title_name
在cmd中輸入
pytest --alluredir ./result/
結(jié)果輸出:
zhongxindeMacBook-Pro:pytest_0 zhongxin$ pytest --alluredir ./result/
===================================================================================== test session starts ======================================================================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/zhongxin/Desktop/pytest_0, inifile:
plugins: splinter-1.9.1, metadata-1.7.0, html-1.19.0, bdd-2.21.0, allure-adaptor-1.7.10
collected 7 items
test_web.py ..F.... [100%]
=========================================================================================== FAILURES ===========================================================================================
____________________________________________________________________________________ test_douban_url[movie] ____________________________________________________________________________________
url = 'https://movie.douban.com', driver = <selenium.webdriver.chrome.webdriver.WebDriver (session="9c0c45ffaf1bc420a9f8ab3753de121d")>
@allure.feature('豆瓣簡單測試')
@allure.story('進(jìn)入頁面')
@pytest.mark.parametrize('url',test_data,ids=['douban','book','movie'])
def test_douban_url(url,driver):
with allure.step('測試進(jìn)入:{}'.format(url)):
driver.get(url)
if url == TEST_URL:
assert driver.title == '豆瓣'
elif url == TEST_BOOK_URL:
assert driver.title == '豆瓣讀書'
else:
> assert 0 #豆瓣電影故意報(bào)錯(cuò)
E assert 0
test_web.py:49: AssertionError
-------------------------------------------------------------------------------------- Captured log call ---------------------------------------------------------------------------------------
remote_connection.py 480 DEBUG POST http://127.0.0.1:50486/session/9c0c45ffaf1bc420a9f8ab3753de121d/url {"url": "https://movie.douban.com", "sessionId": "9c0c45ffaf1bc420a9f8ab3753de121d"}
remote_connection.py 567 DEBUG Finished Request
============================================================================= 1 failed, 6 passed in 93.74 seconds ==============================================================================
將結(jié)果轉(zhuǎn)換為allure報(bào)告报破,并將其展示到瀏覽器上
allure generate ./result/ -o ./report/ --clean
allure open -h 127.0.0.1 -p 8083 ./report/
完整代碼:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:zhongxin
#datetime:2018/8/18 下午10:49
import pytest
from selenium import webdriver
import allure
TEST_URL = 'https://www.douban.com'
TEST_BOOK_URL = 'https://book.douban.com'
TEST_MOVIE_URL = 'https://movie.douban.com'
test_data = [TEST_URL,TEST_BOOK_URL,TEST_MOVIE_URL]
test_book_data = ['掃地出門','朋友之間','觀山海','魚翅與花椒']
@pytest.fixture(scope='module')
def driver():
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(executable_path='./driver/chromedriver',chrome_options=option)
# driver = webdriver.Chrome(executable_path='./driver/chromedriver')
driver.maximize_window()
driver.implicitly_wait(3)
yield driver
driver.close()
@pytest.fixture(name ='book_driver')
def book_driver(driver):
driver.get(TEST_BOOK_URL)
yield driver
driver.get(TEST_BOOK_URL)
@allure.step('搜索:{1}')
def search(driver,message):
driver.find_element_by_id('inp-query').send_keys(message)
driver.find_element_by_xpath('//*[@id="db-nav-book"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()
return driver.title
@allure.feature('豆瓣簡單測試')
@allure.story('進(jìn)入頁面')
@pytest.mark.parametrize('url',test_data,ids=['douban','book','movie'])
def test_douban_url(url,driver):
with allure.step('測試進(jìn)入:{}'.format(url)):
driver.get(url)
if url == TEST_URL:
assert driver.title == '豆瓣'
elif url == TEST_BOOK_URL:
assert driver.title == '豆瓣讀書'
else:
assert 0 #豆瓣電影故意報(bào)錯(cuò)
@allure.feature('豆瓣簡單測試')
@allure.story('豆瓣讀書搜索')
@pytest.mark.parametrize('book',test_book_data)
def test_doubanbook(book,book_driver):
title_name = search(book_driver,book)
assert book in title_name