-
So I am using the raw driver and works perfectly, but want to multi-thread it. Thing is this is a tkinter app packages in a .exe with pyinstaller so I am a little confused on how to use pytest this way. I have a single function that takes arguments that generates the driver and does everything (small function, a few lines only) how could I turn it into a multithreaded function with pytest here? The number of threads being set by a thread safe global variable. Function goes something like this:
Can anyone give me a hand here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
As mentioned here: #2097 (comment) ... If you're looking to run multi-threaded tests in UC Mode with a proxy, here's the scoop: Multi-threading in UC Mode is possible if you use You'll need to use command-line options for this format, eg Below is a sample run command: pytest --uc -n4
Here's a sample file that uses import pytest
@pytest.mark.parametrize("", [[]] * 4)
def test_multi_threaded(sb):
sb.driver.get("https://nowsecure.nl/#relax")
try:
sb.assert_text("OH YEAH, you passed!", "h1", timeout=5.25)
sb.post_message("Selenium wasn't detected!", duration=2.8)
sb._print("\n Success! Website did not detect Selenium! ")
except Exception:
sb.fail('Selenium was detected! Try using: "pytest --uc"') Here's the output when running that file with pytest test_multi_uc.py --uc -n4
============================ test session starts =============================
configfile: pytest.ini
4 workers [4 items]
Success! Website did not detect Selenium!
Success! Website did not detect Selenium!
..
Success! Website did not detect Selenium!
.
Success! Website did not detect Selenium!
.
============================= 4 passed in 9.38s ============================== Some websites may block you if they detect multiple simultaneous connections like that. Be careful where you go. Note that there are different syntax formats. See: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md
If you have to use multithreading without Also, if you haven't already seen the UC Mode video tutorial, then watch https://www.youtube.com/watch?v=5dMFI3e85ig, where you'll see multithreaded UC Mode examples, as well as answers to many of your UC Mode questions. You can also run subprocess.run(["pytest", "-v", "-n", "6", "test_repeat_tests.py"]) Most example tests already have the You can include additional args, such as from seleniumbase import BaseCase
BaseCase.main(__name__, __file__, "-n6", "-v", "-s") That would be the equivalent of running: import subprocess
import sys
if __name__ == "__main__":
subprocess.call([sys.executable, "-m", "pytest", __file__, "-n6", "-v", "-s"]) Using |
Beta Was this translation helpful? Give feedback.
-
Could you explain a little bit better what you mean exactly? I am calling from one thread all the pytests subprocesses, it should work? The issue is that I want to control each pytest test independently, if I run |
Beta Was this translation helpful? Give feedback.
As mentioned here: #2097 (comment) ...
If you're looking to run multi-threaded tests in UC Mode with a proxy, here's the scoop:
Multi-threading in UC Mode is possible if you use
pytest
multi-threading provided by pytest-xdist.You'll need to use command-line options for this format, eg
--uc
to activate UC Mode,-n4
(for 4 parallel processes, etc), and--proxy=user:pass@host:port
to set proxy settings.Below is a sample run command:
Here's a sample file that uses
@pytest.mark.parametrize()
to turn one test into four tests when run withpytest
: