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

标签 python-3.x multithreading selenium pytest python-multithreading

我有一个关于 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/

    相关文章:

    python - 如何使 odoo 计算关系字段在导入时自行计算?

    安装了Python3.5但pip3指向python3.6

    c# - 从另一个线程写入 TextBox?

    c# - 为什么后台线程中的图形操作会阻塞主 UI 线程中的图形操作?

    c# - 合并两个有条件的列表

    testing - 我如何使用 Arquillian 测试带有 JavaScript 确认的按钮单击?

    python - 从字典中查找与今天日期最接近的日期

    python - 使用条件在循环中加入字符串列表中的元素

    C++11 原子。可见性和 thread.join()/停止线程的正确方法

    selenium - 使用带有 127.0.0.1 的内容脚本和带有 Firefox 插件的自定义端口