I have a test script written using pytest and it works fine when I execute it as standalone.
User:~my_workspace/python_project$ pytest path_to_script/my_script.py
Now I have to run this script with different parameters and hence I have created a python file (script_to_invoke_pytest.py) so that I can run the pytest script from this python file.
I tried below approaches within my script_to_invoke_pytest.py
#approach 1
import subprocess
subprocess.Popen(['pytest', r'path_to_script/my_script.py'], shell=True)
#approach 2
import pytest
pytest.main([r'path_to_script/my_script.py'])
Both the approaches didn't work and I get errors related no modules named xyz.
Im using pycharm ide and before running pytest, I execute the docker.
Please let me know how can I invoke my pytest from my python script script_to_invoke_pytest.py
In your second approach, you're not telling python where to find the tests. Try:
import pytest
pytest.main([r'path_to_script/my_script.py'])
Related
I am trying to use pytest within VS code, running in red hat linux. The environment that I am using means that I need to load modules such as pandas before running pytest. In the terminal I can run:
module load pandas
pytest
and the tests are successfully run. I can do this in both the standard terminal, in the Python Debug Console within VS code, and int he bash terminal within VS code. If, however, I press the "Run All Tests" button within VS code, then I just get an error telling me that it cannot find the pandas module.
How can I tell the test environment to run my module load pandas command before running pytest?
In this case I would create a file named conftest.py in the directory containing the tests. pytest automatically executes this file before the tests are run. In this file you could have Python execute shell commands. For the latter there are different options, but first try one of the easier ones.
More information on conftest.py:
In pytest, what is the use of conftest.py files?
The traceback provided by pytest is great and super useful for debugging.
Is there a way to run a script using the pytest api even if the script itself does not contain any test modules? Essentially, I would like a way to pinpoint and run a certain function in a script as if it were a test, but get the pytest-formatted traceback.
The pytest documentation on test discovery states that normally only functions whose name begins with test_ are run. This behaviour can be changed however with the python_functions configuration option. Try entering in the command line:
pytest [script.py] -o python_functions=[script_function]
in which you should replace [script.py] with your python script file path and replace [script_function] with the name of the function that you want to be run.
I have 2.3.3 version of pytest running on windows. I have a test folder which contains bunch of test files like test1.py, test2.py, test3.py etc. If i open command prompt and navigate to this folder to run a particular test
pytest test1.py
Instead of just running test1.py, it is running all the tests in the folder. Like test1.py, test2.py, test3.py etc.
So pytest is not taking arguments and parsing them. I am seeing this only on windows. Does anyone know what is happening here?
Thanks a bunch in advance.
I can't check this but what I'd do first would be check PATH for the pytest executable. I'd except a Windows batch script, and continue investigation in the code, maybe that's where the args are lost or passed (quoted?) incorrectly.
When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.
I'm trying to run a selenium testcase in python. I have testcases that I can run directly from the command line no problem with
python seleniumtest.py
However when I try to run it from within python it is failing.
__import__('seleniumtest')
seleniumtest.py ends with a command
unittest.main()
this command seems to fail when it gets run using the __import__ method. Does know why running this through the import is not working? For my purposes I cannot simply use popen.
You need to have something like this at the end of your selenium program:
if __name__=="__main__"