1.python终端执行pip install pytest-xdist

2.创建conftest.py(pytest默认读取conftest.py里面的配置)

import pytest
from selenium import webdriver

@pytest.fixture(scope="session")
def setup(request):
    print("initiating chrome driver")
    driver = webdriver.Chrome(r'C:/Users/MNN/AppData/Local/Google/Chrome/Application/chromedriver.exe')
    session = request.node
    for item in session.items:
        cls = item.getparent(pytest.Class)
        setattr(cls.obj, "driver", driver)
    driver.get("http://www.baidu.com")
    driver.maximize_window()

    yield driver
    driver.close()

3.创建test_example1.py

import pytest

@pytest.mark.usefixtures("setup")
class TestExampleOne:
    def test_title(self):
         assert "百度一下,你就知道" in self.driver.title

4.创建test_example2.py

import pytest
import time
@pytest.mark.usefixtures("setup")
class TestExampleTwo:

    def test_InputForm(self):

        print("Another example")
        mainMenu = self.driver.find_element_by_xpath("//a[contains(text(),'新闻')]")
        print(mainMenu.text)
        assert "新闻" in mainMenu.text

    def test_funcfast(self):
        time.sleep(0.1)

    def test_funcslow1(self):
        time.sleep(0.2)

    def test_funcslow2(self):
        time.sleep(0.3)

5.在pycharm终端执行命令:pytest -s -v -n=2

注:更多内容可参考以下博客:
博客1: 链接.

Logo

一站式 AI 云服务平台

更多推荐