ImportError: No module named Binary with pytest - python

I'm working through http://blog.thedigitalcatonline.com/blog/2015/05/13/python-oop-tdd-example-part1/#.Vw0NojFJJ9n .
When I try:
$ py.test
============================= test session starts =============================
platform win32 -- Python 3.2.5, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: C:\envs\r3\binary, inifile:
plugins: capturelog-0.7
collected 0 items / 1 errors
=================================== ERRORS ====================================
____________________ ERROR collecting tests/test_binary.py ____________________
tests\test_binary.py:3: in <module>
import Binary
E ImportError: No module named Binary
================= 1 pytest-warnings, 1 error in 0.20 seconds ==================
What am I doing wrong?

Add current directory to PYTHONPATH environmental variable.
As you are on Windows:
$ set PYTHONPATH="."
This shall help py.test to find and import the module.
Checking the py.test tutorial I see, that at "Writing the class" section they use exactly the same trick.
In practice, you do not have to do this, as you usually test against installed Python module (typically with setup.py in project root directory and using develop mode), and it is accessible for import easily without playing with PYTHONPATH.

Related

How to view runtime warnings in PyCharm when running tests using pytest?

When running tests in PyCharm 2022.3.2 (Professional Edition) using pytest (6.2.4) and Python 3.9 I get the following result in the PyCharm console window:
D:\cenv\python.exe "D:/Program Files (x86)/JetBrains/PyCharm 2022.3.2/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path D:\tests\test_k.py
Testing started at 6:49 PM ...
Launching pytest with arguments D:\tests\test_k.py --no-header --no-summary -q in D:\tests
============================= test session starts =============================
collecting ... collected 5 items
test_k.py::test_init
test_k.py::test_1
test_k.py::test_2
test_k.py::test_3
test_k.py::test_4
======================= 5 passed, 278 warnings in 4.50s =======================
Process finished with exit code 0
PASSED [ 20%]PASSED [ 40%]PASSED [ 60%]PASSED [ 80%]PASSED [100%]
So the actual warnings don't show. Only the number of warnings (278) is shown.
I tried:
selecting: Pytest: do not add "--no-header --no-summary -q" in advanced settings
Setting Additional arguments to -Wall in the Run/Debug configurations window
Setting Interpreter options to -Wall in the Run/Debug configurations window
and all permutations, all to no avail. Is there a way to show all runtime warnings when running tests using pytest in PyCharm in the PyCharm Console window?
EDIT:
#Override12
When I select do not add "--no-header --no-summary -q" in advanced settings I get the following output:
D:\Projects\S\SHARK\development_SCE\cenv\python.exe "D:/Program Files (x86)/JetBrains/PyCharm 2020.3.4/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path D:\Projects\S\SHARK\development_SCE\cenv\Lib\site-packages\vistrails-3.5.0rc0-py3.9.egg\vistrails\packages\SHARK\analysis\tests\test_fairing_1_plus_k.py -- --jb-show-summary
Testing started at 10:07 AM ...
Launching pytest with arguments D:\Projects\S\SHARK\development_SCE\cenv\Lib\site-packages\vistrails-3.5.0rc0-py3.9.egg\vistrails\packages\SHARK\analysis\tests\test_fairing_1_plus_k.py in D:\Projects\S\SHARK\development_SCE\cenv\Lib\site-packages\vistrails-3.5.0rc0-py3.9.egg\vistrails\packages
============================= test session starts =============================
platform win32 -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\Projects\S\SHARK\development_SCE\cenv\python.exe
cachedir: .pytest_cache
rootdir: D:\Projects\S\SHARK\development_SCE\cenv\Lib\site-packages\vistrails-3.5.0rc0-py3.9.egg\vistrails\packages
plugins: pytest_check-1.0.5
collecting ... collected 5 items
SHARK/analysis/tests/test_fairing_1_plus_k.py::test_init
SHARK/analysis/tests/test_fairing_1_plus_k.py::test_without_1_k_fairing
SHARK/analysis/tests/test_fairing_1_plus_k.py::test_1_k_fairing_given
SHARK/analysis/tests/test_fairing_1_plus_k.py::test_without_1_k_fairing_only_3_values_under_threshold
SHARK/analysis/tests/test_fairing_1_plus_k.py::test_1_k_fairing_given_only_3_values_under_threshold
============================== warnings summary ===============================
......\pyreadline\py3k_compat.py:8
D:\Projects\S\SHARK\development_SCE\cenv\lib\site-packages\pyreadline\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
return isinstance(x, collections.Callable)
......\nose\importer.py:12
D:\Projects\S\SHARK\development_SCE\cenv\lib\site-packages\nose\importer.py:12: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
from imp import find_module, load_module, acquire_lock, release_lock
SHARK/analysis/tests/test_fairing_1_plus_k.py: 276 warnings
D:\Projects\S\SHARK\development_SCE\cenv\lib\site-packages\pymarin\objects\key.py:1101: UserWarning: siUnits is deprecated, use siUnit
warnings.warn('siUnits is deprecated, use siUnit')
-- Docs: https://docs.pytest.org/en/stable/warnings.html
======================= 5 passed, 278 warnings in 5.79s =======================
Process finished with exit code 0
PASSED [ 20%]PASSED [ 40%]PASSED [ 60%]PASSED [ 80%]PASSED [100%]
So 4 warnings are displayed. However I would like to see all 278 warnings.
When I run pytest from the command line outside PyCharm I get the same result. So it seems to be a pytest problem and it seems that it has nothing to do with PyCharm.
I think you could also try adding verbosity or collect warnings in a programatic way, by changing how you execute the tests.
The solution is a combination of two things:
Setting 'do not add "--no-header --no-summary -q"' in advanced settings as #Override12 suggested.
When the same warning is issued multiple times, only the first time is displayed. In my case solving the first warning reduced the number of warnings from 278 to 2.

pytest - Suppress DeprecationWarning from specific 3rd party modules

When I run pytest I'm getting some deprecation warnings from a 3rd party library. I'd like to be informed about any deprecation warnings in my own code, but not in a vendored copy of a library bundled with another 3rd-party library.
This answer was helpful in getting me partway there. If I run pytest like this:
$ pytest ./tests/ I get:
$ pytest ./tests/
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items
tests/test_file1.py . [ 20%]
tests/test_file2.py .... [100%]
=============================== warnings summary ===============================
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Mapping, MutableMapping
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 5 passed, 2 warnings in 2.54s =========================
but if I run pytest like this: $ pytest ./tests/ -W ignore::DeprecationWarning I get:
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items
tests/test_file1.py . [ 20%]
tests/test_file2.py .... [100%]
============================== 5 passed in 2.61s ===============================
This second output shows me that the filter works, but that will also hide any deprecation warnings I'd like to seeing resulting from my own code.
Part of this issue is that I'm not sure which module to try referencing in the ignore filter. I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:urllib3.*: and I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:botocore.*:. Both of these result in the same output as the first example with no filtering.
How can I filter out DeprecationWarnings from the version of urllib3 packaged with the vendored version of requests included with botocore (which gets called when I run commands with the boto3 library)?
You should use the warning filters options (ini or marks):
[pytest]
filterwarnings =
ignore::DeprecationWarning:botocore.*:
Source: https://docs.python.org/3/library/warnings.html#default-warning-filter
"Individual warnings filters are specified as a sequence of fields separated by colons:"
action:message:category:module:line
The answer from #Santiago Magariños does not work for me (but it put me on the correct path to find solution - so many thanks).
I use Python 3.9, pytest 6.2.1 and trying to suppress warnings from selenium 3.141.0.
I have realized that I need to prepend .* before the module name:
[pytest]
filterwarnings = ignore:::.*.selenium
or use the full "path". So to suppress the warning
../../../../../../.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28
/home/vaclav/.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28: DeprecationWarning: invalid escape sequence \
"""Constructor, takes a WebDriver instance and timeout in seconds.
I need to use this filter in pytest.ini file:
[pytest]
filterwarnings = ignore:::.home.vaclav..local.share.virtualenvs.common-bjARi2zp.lib.python3.9.site-packages.selenium
if you are using pyproject.toml file for pytest configuration you can use:
[tool.pytest.ini_options]
testpaths = ["./tests/unit"]
filterwarnings = ["ignore:::.*third_party_package.module:123", "ignore:::.*another_module*"]
ignore:::.*third_party_package.module:123 ignores in specific warning at specific line
ignore:::.*another_module* ignores all warnings in the module.
Notice that you can have multiple ignores. You need to list them in []. Also, you can't have *third_party_package/module:123 you must replace all / with .

Pytest not detecting FizzBuzzTest.py

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():

pytest with multiple jobs works, but create issue when use single job

I am using Mac. I installed tox to test my app.
When I use pytest with -n0 option, it gives error for import module
root#localhost:myapp root$ .tox/py35/bin/py.test -n0
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
rootdir: /Users/root/myapp, inifile:
plugins: xdist-1.20.1, mock-1.6.3, forked-0.2, cov-2.2.1, celery-4.0.2
collected 2 items / 2 errors
===================================================================================== ERRORS ======================================================================================
__________________________________________________________________ ERROR collecting app/tests/unit/test_utils.py __________________________________________________________________
ImportError while importing test module '/Users/root/myapp/app/tests/unit/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
app/tests/unit/test_utils.py:1: in <module>
from app import utils
E ImportError: No module named 'app'
__________________________________________________ ERROR collecting app/tests/unit/automation/collections/test_collection.py __________________________________________________
ImportError while importing test module '/Users/root/myapp/app/tests/unit/automation/collections/test_collection.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
app/tests/unit/automation/collections/test_collection.py:3: in <module>
from app.automation.collections import collection
E ImportError: No module named 'app'
============================================================================= pytest-warning summary ==============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the #pytest.fixture decorator instead.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=================================================================== 1 pytest-warnings, 2 error in 1.27 seconds ====================================================================
But same command works with -n5 option
root#localhost:myapp root$ .tox/py35/bin/py.test -n5
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
rootdir: /Users/root/myapp, inifile:
plugins: xdist-1.20.1, mock-1.6.3, forked-0.2, cov-2.2.1, celery-4.0.2
gw0 [9] / gw1 [9] / gw2 [9] / gw3 [9] / gw4 [9]
scheduling tests via LoadScheduling
.........
What it set in -n5 which is not set for -n0 ?

Py.Test aborts when (py)ROOT is imported

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

Categories

Resources