I am running a small test py.test script, and I want to determine the name of the test script being used.
The current script is run as
pytest selenium/test_morph_viz.py -s
but later it is supposed to be run without the argument. Part of the output of the run is as follows:
platform linux2 -- Python 2.7.12, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /home/adietz/Projects/Invest/bsp-usecase-tests, inifile:
collected 1 item
selenium/test_morph_viz.py E
Within the code I define a setup method in which I want to get the name of the test script, i.e.
selenium/test_morph_viz.py
How to do that?
I tried to use inspect or os.path.basename, but this only returns the path of a script in which I define other things. So how to get the name of the script currently run in py.test?
Found the correct answer by chance:
self.__module__
That gives you the module currently used.
Related
I have problem in importing pytest while writing a python code. "import pytest is grayed out.
Python is 3.8.3, Pycharm community edition.
pytest version 5.4.2, is successfully installed and can be seen in the project interpreter in pycharm. As well as I can see the installed path of pytest in python directory.
When running py.test command from console. It starts the test run shows "collected 0 items" and lastly ends with "NO TESTS RAN IN 0.05s"
If anyone running similar problems with some other packages kindly let me know.
TIA...
You simply run pytest from the commandline. There is no need to import pytest into a script. Take this Python script as an example:
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 4
To run pytest on it, from the terminal (after changing to the right directory):
$ pytest
And you will then see the test outcome in the commandline as pytest automatically picks up the python scripts names test_*.py, where * is any name, e.g. test_increment.py. To have a test from your Python script run, name it with test_ as well to begin with.
Running pytest in the terminal is an option. In addition, Pycharm has integrated test suite for automatic discovery and collection of test tasks. You can use hotkey ctrl+shift+10 to run the test tasks directly in current file .
I’m pretty new to contributing to open source projects and am trying to get some coverage reports so I can find out what needs more / better testing. However, I am having trouble getting the full coverage of a test. This is for pytorch
For example, lets say I want to get the coverage report of test_indexing_py.
I run the command:
pytest test_indexing.py --cov=../ --cov-report=html
Resulting in this:
================================================= test session starts =================================================
platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Projects\pytorch
plugins: hypothesis-5.4.1, arraydiff-0.3, cov-2.8.1, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2
collected 62 items
test_indexing.py ............................s................................. [100%]
----------- coverage: platform win32, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov
=========================================== 61 passed, 1 skipped in 50.43s ============================================
Ok, looks like the tests ran. Now when I check the html coverage report, I only get the coverage for the test file and not for the classes tested (the tests are ordered by coverage percentage).
As you can see, I am getting coverage for only test_indexing.py. How do I get the full coverage report including the classes tested?
Any guidance will be greatly appreciated.
I think its because you are asking to check the coverage from the test running directory, ie where test_indexing.py is.
A better approach would be like running the test from the root directory itself, rather than test directory, it has several advantages like the configuration file reading and all.
And regarding your question, try running the test from the root directory and try
pytest path/to/test/ --cov --cov-report=html
This is my code in FizzBuzzTest.py
import pytest
# content of test_sample.py
def fizzBuzz(value):
return value
def test_returns1With1PassedIn():
assert fizzBuzz(1) == 1
When I run pytest -v in the command line
================================= 1 passed in 0.05 seconds ===================================================================
PS C:\Users\pytest\FizzBuzz_Kata> pytest -v
======================================== test session starts ================================================================================================
platform win32 -- Python 3.5.2, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- c:\users\a606143\appdata\local\programs\python\python35\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\pytest\FizzBuzz_Kata
collected 0 items
Could somebody please explain why pytest is not able to detect this file and run tests?
I have fiddled a little around and I seem to have found a solution, a little odd but it is working.
To have pytest to detect the file, it needs to be name with test_ infront of the filename, so your file should be named: test_FizzBussTest.py and your function which pytest have to execute needs to have that exact same name, so your function needs to be named as such:
def test_FizzBussTest():
Edit: After further researched, the function doesnt need to be named exactly as the file, it just needs to have test_ infront of the function name so fx test_sum():
i have the following situation which i don't understand maybe you can point me to the answer or explain it to me.
I have the following python file structure structure:
project/
-folder_a/
-File_a
-folder_b/
-File_b
File_a is importing File_b. File_a is the main file but i can only run it from the project folder if i call it like this.
python < folder_a/File_a
Otherwise i get an import error that the File_b can not be imported. I know that the "<" symbol is a rediret of the stdin but what is it doing to the python interpreter and why is it only working this way.
Thanks a lot,
make-ing
Python can run code a few different ways: you can give it a script to run, or a module with -m, or a command with -c. But if you don't give it any of those, it reads standard input and executes one statement at a time until EOF.
You're used to seeing this with the interactive interpreter:
$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello
>>> ^D
$
It read that print('hello') off standard input and executed it. Then it read the ctrl-D as an EOF and exited.
If standard input isn't an interactive console (effectively, if not sys.stdin.isatty():), it doesn't print the banner, show the >>> prompts, enable readline command-line editing, etc. But it still reads and executes statements one by one until EOF.
When you do python < something.py, your shell is piping the file something.py into Python's standard input. Since that file isn't an interactive console, Python doesn't do all the interactive stuff; it just reads and executes statements out of the script.
This is similar to running python something.py, but not identical.
The biggest difference is that Python has no idea what script you're giving it; it can only see the contents of the file, not the filename or anything else, and it can't even tell they're coming from a file rather than being, e.g., piped from another program.
If you look at how sys.path works:
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
So, in effect, python folder_a/File_a.py puts ./folder_a on sys.path, but python < folder_a/File_a.py puts . on sys.path.
This is really not a good solution to your problem, but it doesn't explain why things are mostly working.
A better solution is to reorganize your code so that you have packages full of modules you want to import, and then any top-level scripts that you want to run outside of those packages. Like this:
project/
script.py
-pkg_a/
-__init__.py
-module_a.py
-pkg_b/
-__init__.py
-module_b.py
Those __init__.py files aren't actually necessary in Python 3, but they signal (both to the Python interpreter, and to your reader) that these are "ordinary packages" (as opposed to namespace packages, or directories that aren't packages at all).
Now, script.py can import and run the code from module_a.py the same as any other Python code. For example, instead of this:
# pkg_a/module_a.py
print('hello')
… do this:
# pkg_a/module_a.py
def run():
print('hello')
# script.py
from pkg_a.module_a import run
run()
If you plan to use setuptools to make your code installable via pip, you can go even farther—specify pkg_a.module_a.run as an "entry point", and pip will create that script.py for you, make sure it executable, set up the shbang line for the user's particular Python, get it installed somewhere on the user's path, etc.
If something about your design makes it impossible or not appropriate to move the "script" code out of your module and into a separate script, you can always just run it as a module, the same way you do with the ones in the stdlib:
$ python -m pip install spam
<installs the spam package>
$ echo '[{"dense":"json"}]' | python -m json.tool
[
{
"dense": "json"
}
]
$ python -m pkg_a.module_a
<runs the code in pkg_a/module_a.py as a module>
I've a problem unit testing Python code, which uses the (py)ROOT package. It simply aborts collecting the tests when there is a line import ROOT in one of the scripts.
Does anybody have an idea what's going on?
foo#bar ~/project/dir [19:21:17]
(project)> $ py.test -v [±master ●]
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.25 -- pytest-2.6.3 -- /path/to/.virtualenvs/project/bin/python
collecting 0 items[1] 16520 abort py.test -v
I had a similar issue by adding the PyROOT bindings to export $PYTHONPATH in my .bashrc. The locally set PYTHONPATH was ignored. A solution is copying or linking the ROOT.py and libPyROOT.so files to the appropriate Python directory in /usr/lib. More details in this answer: https://stackoverflow.com/a/33130000/4753851