Parameterization and data driver

Parameterization and data driver


Abstract:
parameterized using
pytest.mark.parametrize () directly transmitted reference
data driving
external file: Use yaml
test parameter acquired from an external file
test step obtaining from an external file


Use parameterized

@ pytest.mark.parametrize ( "search_words", [ ' game', "hhdddx"])
Specific use cases:

from page.app import App


class TestDemo:

    def setup(self):
        self.main_page = App.start()

    @pytest.mark.dependency()
    def test_game_handle(self):
        '''点击首页的游戏手柄,进入游戏中心'''
        self.game_center = self.main_page.click_game_handle()

   # @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
    @pytest.mark.parametrize("keyword", ['游戏', "hhdddx"])
    def test_game_search(self, keyword):
        self.test_game_handle()
        '''点击游戏中心页右上角的搜索,进行搜索查询'''
        self.game_center.search(keyword)
        sleep(5)
        self.game_center.get_first_content(keyword)


    def test_password_login(self):
        '''从游戏中心-我的进入登录页面,进行登录'''
        self.test_game_handle()
        self.my_page = self.game_center.click_bottom_button_mine()
        self.mobile_login = self.my_page.click_no_login_content()
        self.password_login = self.mobile_login.switch_to_password_login()
        self.password_login.password_login()


    def teardown(self):

        sleep(20)
        App.quit()

Here will restart several times APP, in order not to restart the APP, is not used setup and teardown, but the use of setupclass

Data-driven

Parametric data read external file: use yaml, JSON reading
test step of reading an external file: custom execution engine
assert step of reading an external file: custom execution engine
entire use case reads an external file: dynamically create use cases

Parametric data read external file:
Example: Use yaml file
1 yaml installed
using installation tools pycharm

computer windows - open pycharm-setting-project-Project Interpreter- click the plus sign - Find pyyaml ​​be installed in the search box

Use command to install, the installation package obtained from the source pypi
pip3 install pyyaml

Use command to install, use domestic source for installation packages
pip3 install pyyaml -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip the use of domestic sources to download the installation package: PIP use of domestic sources to download the installation package
yaml use of tutorials: Click
2 Import YAML
Import YAML
search_data = yaml.salf_load (Open ( "search.yaml", "r"))
Print (search_data)

from time import sleep

import pytest
import yaml

from page.app import App


class TestDemo:
    search_data = yaml.safe_load(open("search.yaml", "r"))

    def setup(self):
        self.main_page = App.start()

    @pytest.mark.dependency()
    def test_game_handle(self):
        '''点击首页的游戏手柄,进入游戏中心'''
        self.game_center = self.main_page.click_game_handle()

   # @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
    @pytest.mark.parametrize("keyword", search_data)
    def test_game_search(self, keyword):
        self.test_game_handle()
        '''点击游戏中心页右上角的搜索,进行搜索查询'''
        self.game_center.search(keyword)
        sleep(5)
        self.game_center.get_first_content(keyword)

search.yaml file

- ['游戏']
- [game]

Reference article:

pip the use of domestic sources to download and install packages ==: pip use of domestic sources to download the installation package
yaml use of tutorials: Click to view

Test step of reading an external file


Reference article:
Video: Video Click on the link to jump
article:
Junit5 + YAML easy parameterization and data-driven, so App automated testing more efficient (a) Hit the jump
* Junit5 + YAML easy parameterization and data-driven, automated let App tests are more efficient (two)
click to jump

Published 99 original articles · won praise 43 · views 160 000 +

Guess you like

Origin blog.csdn.net/mayanyun2013/article/details/104611183