gpt4 book ai didi

python-3.x - 用线程运行 pytest

转载 作者:行者123 更新时间:2023-12-01 19:36:57 25 4
gpt4 key购买 nike

我有一个关于 pytest 的问题

我想用多个线程运行相同的 pytest 脚本。
但是,我不确定如何创建和运行传递多个参数的线程。 (并使用 pytest 运行线程..)

例如我有
test_web.py

from selenium import webdriver
import pytest


class SAMPLETEST:
self.browser = webdriver.Chrome()
self.browser.get(URL)
self.browser.maximize_window()


def test_title(self):
assert "Project WEB" in self.browser.title


def test_login(self):
print('Testing Login')

ID_BOX = self.broswer.find_element_by_id("ProjectemployeeId")
PW_BOX = self.broswer.find_element_by_id("projectpassword")
ID_BOX.send_keys(self.ID) # this place for ID. This param come from thread_run.py
PW_BOX.send_keys(self.PW) # this place for PW. It is not working. I am not sure how to get this data from threa_run.py
PW_BOX.submit()

在 thread_run.py 中
import threading
import time
from test_web import SAMPLETEST

ID_List = ["0","1","2","3","4","5","6","7"]
PW_LIST = ["0","1","2","3","4","5","6","7"]



threads = []
print("1: Create thread")

for I in range(8):
print("Append thread" + str(I))
t = threading.Thread(target=SAMPLETEST, args=(ID_List[I], PW_LIST[I]))
threads.append(t)


for I in range(8):
print("Start thread:" + str(I))
threads[I].start()

我能够在没有 pytest 的情况下运行线程来运行许多 SAMPLETEST 类。

但是,它不适用于 pytest。

我的问题是。

首先,如何在SAMPLETEST的insde中初始化self.brower?我确定下面的代码将不起作用
self.browser = webdriver.Chrome()
self.browser.get(URL)
self.browser.maximize_window()

其次,在 thread_run.py 中,当我运行线程以在 test_web.py 上调用 SAMPLTEST 时,如何传递两个参数(ID 和密码)?

ID_BOX.send_keys(self.ID) # 这个地方为ID。这个参数来自 thread_run.py
ID_BOX.send_keys(self.ID) 

PW_BOX.send_keys(self.PW)

我试图在 SAMPLETEST 类中构建构造函数( init )但它不起作用......

我不太确定如何使用 pytest 运行线程(传递参数或参数)。

最佳答案

有 2 个场景,我可以从中读取:

  • 准备测试数据并将参数传递给您的测试方法,这可以通过 pytest-generate-tests 和参数化概念来实现。可以引用文档here
  • 如果在多线程中运行 pytest - Pytest-xdistpytest-parallel
  • 关于python-3.x - 用线程运行 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54155066/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com