Running pytest with thread

Viewed 9620

I have a question for pytest

I would like to run same pytest script with multiple threads. But,i am not sure how to create and run thread which is passing more than one param. (And running thread with pytest..)

for example I have 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()

i was able to run thread to run many SAMPLETEST class without pytest.

However, it is not working with pytest.

My question is.

First, how to initialize self.brower in insde of SAMPLETEST? I am sure below codes will not be working

self.browser = webdriver.Chrome()
    self.browser.get(URL)
    self.browser.maximize_window()

Second, in thread_run.py, how can i pass the two arguments(ID and Password) when I run thread to call SAMPLTEST on test_web.py?

ID_BOX.send_keys(self.ID) # this place for ID. This param come from thread_run.py

ID_BOX.send_keys(self.ID) 

            PW_BOX.send_keys(self.PW) 

I was trying to build constructor (init) in SAMPLETEST class but it wasn't working...

I am not really sure how to run threads (which passing arguments or parameter ) with pytest.

2 Answers

There are 2 scenarios which i could read from this:

  1. prepare the test data and pass in parameters to your test method which could be achieved by pytest-generate-tests and parameterise concept. You can refer to the documentation here
  2. In case of running pytest in multi threading - Pytest-xdist or pytest-parallel

I had a similar issue and got it resolved by passing an argument in form of a list.

Like:, I replaced below line

thread_1 = Thread(target=fun1, args=10)

with

thread_1 = Thread(target=fun1, args=[10])

Related