【问题标题】:Running pytest with thread使用线程运行 pytest
【发布时间】:2019-06-06 21:07:24
【问题描述】:

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

IN 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 运行线程(传递参数或参数)。

【问题讨论】:

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


    【解决方案1】:

    我可以从中看出两种情况:

    1. 准备测试数据并将参数传递给您的测试方法,这可以通过 pytest-generate-tests 和参数化概念来实现。可以参考文档here
    2. 如果在多线程中运行 pytest - Pytest-xdistpytest-parallel

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多