PyTest管理自動化測試用例

一、 基本應用

1、如下代碼是對日報的增加、查看、修改和刪除:(文件名稱:test_dailyreport.py)

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
caseName:工作日報
'''
# import unittest
import pytest
from businessView.daily_report import DailyReportPage
from baseView.browser_view import Browser
from common.get_configInfo import get_config
from businessView.login import LoginView
from common.get_log import get_log
log = get_log()

save_data = [("2019-07-03", "測試數據01", "0")]
update_data = [("2019-07-01", "測試數據-修改", "0"), ]


class TestDailyReport(object):

    '''
    1、用fixture在所有用例開始前初始化測試對象、在所有用例結束後銷燬測試對象
    2、yield是用於生成器,與return不同!所有用例執行完後,會執行yield後面的代碼,
        無論用例是否執行成功,該代碼都會執行(相當於teardown)
    3、在一個文件中,用例的執行順序是從上到下的
    '''
    @pytest.fixture(scope="class")
    def init_dailyreport(self):
        # 打開瀏覽器和url
        browser = Browser()
        driver = browser.open_browser()

        # 登錄(以下用例的前置條件一致,均需要登錄,所以放到該方法中)
        username = get_config("accountInfo", "username")
        password = get_config("accountInfo", "password")

        lv = LoginView(driver)
        lv.login_action(username, password)
        lv.into_report_page()

        # 初始化工作日報界面類
        drp = DailyReportPage(driver)
        yield drp

        # 以下相當於teardown代碼,不管用例執行成功與否,該代碼都會執行,完美
        log.info("Close the browser.")
        # quit關閉瀏覽器後,會自動刪除臨時文件夾,不要用close
        driver.quit()

    '''
    1、用例調用fixture的返回值,直接把函數名稱當做變量名稱,比如:setup_teardown
    2、測試用例參數化用pytest.mark.parametrize,注意測試數據的格式
    3、pytest.mark.usefixtures只能用於沒有返回值的fixture
    '''
    # 暫存日報用例
    @pytest.mark.parametrize("date,content,hours", save_data)
    def test_save(self, init_dailyreport, date, content, hours):

        log.info("******** 用例名稱:暫存日報 ********")
        init_dailyreport.save(date, content, hours)

    @pytest.mark.add
    # 查看日報用例
    def test_check(self, init_dailyreport):
        log.info("******** 用例名稱:查看日報 ********")
        init_dailyreport.check()

    # 修改日報用例
    @pytest.mark.parametrize("update_date, update_content, update_hours", update_data)
    def test_update(self, init_dailyreport, update_date, update_content, update_hours):
        log.info("******** 用例名稱:修改日報 ********")
        init_dailyreport.update(update_date, update_content, update_hours)

    # 刪除日報用例
    def test_delete(self, init_dailyreport):
        log.info("******** 用例名稱:刪除日報 ********")
        init_dailyreport.delete()


if __name__ == "__main__":

    # 運行該文件中所有用例
    pytest.main(['-s', "test_dailyreport.py"])

    # 運行單個用例
    # pytest.main(['-s', "test_dailyreport.py::TestDailyReport::test_save"])

    # 運行某些被標記的用例
    # pytest.main(['-s', "test_dailyreport.py", "-m=add"])

    # 運行某些未被標記的用例(除了標記爲add之外的用例)
    # pytest.main(['-s', "test_dailyreport.py", "-m=not add"])

    # 該命令表示運行該文件所在路徑下的所有測試文件
    # pytest.main()


2、fixture的應用:

  1)類似於unittest中的setUp和tearDown函數的應用,用來初始化測試對象和銷燬測試對象;

  2)用scope來控制fixture的作用範圍:

       class代表作用於該類中的所有用例。即該類中所有用例執行前和執行後;

       function代表每個用例執行前和執行後;

       還有module和session,使用時在網上查找使用方法即可

  3)其中yeild不同於return,yeidl返回了實例化的對象drp,供其他測試用例調用;

  4)yeild後面放置的代碼相當於tearDown函數中代碼,不管用例執行是否成功,在用例執行結束後均會執行該代碼

3、用例調用fixture的返回值:直接把函數名作爲參數和變量使用

4、使用pytest.mark.parametrize來管理測試用例數據,第一個參數是用例中參數列表,第二個參數是參數對應的值

5、運行用例的多種方式:

1)運行該文件中所有用例

pytest.main(['-s', "test_dailyreport.py"])

2)運行單個用例(類中的某個函數)

pytest.main(['-s', "test_dailyreport.py::TestDailyReport::test_save"])

3)用mark標記用例,並執行這些被標記的用例

pytest.main(['-s', "test_dailyreport.py", "-m=add"])
pytest.main(['-s', "test_dailyreport.py", "-m=check or add"])

4)執行除了被標記之外的其他用例

pytest.main(['-s', "test_dailyreport.py", "-m=not add"])

5)運行該文件所在路徑下的所有測試文件,比如test_login.py與該文件在同一路徑下,則如下命令會執行這兩個文件中所有用例

pytest.main()

6、命名規則:

1)文件名:test_*.py或者*_test.py

2)類:Test開頭

3)函數:test_開頭

二、僅執行一次登錄和退出操作

1、在測試用例所在的文件夾下新建文件conftest.py

import pytest
from baseView.browser_view import Browser
from businessView.login import LoginView
from common.get_configInfo import get_config
from common.get_log import get_log
log = get_log()


@pytest.fixture(scope="session")
def login():
    # 打開瀏覽器和url
    browser = Browser()
    driver = browser.open_browser()
    # 登錄
    lv = LoginView(driver)
    username = get_config("accountInfo", "username")
    password = get_config("accountInfo", "password")
    lv.login_action(username, password)
    yield driver
    # 以下相當於teardown代碼,不管用例執行成功與否,該代碼都會執行
    # 關閉瀏覽器
    log.info("Close the browser.")
    driver.quit()

1)該文件用於編寫多個測試用例文件的fixture功能。比如:我只想登錄一次,執行多個模塊的用例,則可以用到該文件

2)注意該文件中的函數的fixture範圍爲“session”,作用於所有測試用例文件

3)文件名稱是固定的,且不需要導入該文件,pytest可自行找到

2、結合工作日報界面的用例文件:

'''
caseName:工作日報
'''
import pytest
import allure
from businessView.daily_report import DailyReportPage
from common.get_log import get_log
log = get_log()

save_data = [("2019-07-03", "測試數據01", "0")]
update_data = [("2019-07-01", "測試數據-修改", "0"), ]


@allure.feature('工作日報')
@pytest.mark.run(order=1)
class TestDailyReport(object):

    @pytest.fixture(scope="class")
    def init_dailyreport(self, login):
        # 初始化工作日報界面類
        drp = DailyReportPage(login)
        # 進入工作日報界面
        drp.into_report_page()
        yield drp
        drp.tab_close()

    @allure.story('暫存日報')
    @pytest.mark.flaky(reruns=3, reruns_delay=2)
    @pytest.mark.parametrize("date,content,hours", save_data)
    def test_save(self, init_dailyreport, date, content, hours):
        log.info("******** 用例名稱:暫存日報 ********")
        init_dailyreport.daily_report_save(date, content, hours)
        assert init_dailyreport.dailyReport_check_save()

1)注意看init_dailyreport函數裏面的參數“login”,就是取自文件conftest.py的函數名稱(因爲需要用到返回值)

2)測試用例中也可以設置fixture功能,用來初始化界面類

3)若想設置用例的執行順序,則需要安裝插件pytest-ordering,然後在用例類上添加@pytest.mark.run(order=1)

其中order的數值越小優先級越高

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章