pytest seems to be ignoring tests that use PySide.
For example, I have a file test_pyside.py with the contents
from PySide import QtGui
def test_dialog():
dialog = QtGui.QDialog()
assert False
The output for running python -m pytest test_pyside.py is:
============================ test session starts ============================
platform win32 -- Python 2.7.12, pytest-3.0.5, py-1.4.31, pluggy-0.4.0 rootdir: C:\Users\erik, inifile:
collected 1 items
That's it. No Error report. I don't know why.
Your test segfaults because it creates a QDialog without creating a QApplication. I suggest using the pytest-qt plugin to take care of that for you.
Related
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.
I've been working on a PR for rpy2 package to improve its support for Windows, and I cannot figure out how to debug the pytest-cov/coverage related issue on a test.
Here is the link to my folk for the test, which only hangs in Windows (passes GitHub Action with Ubuntu & MacOS). Eliminating irrelevant options, the minimal pytest command is:
#!/bin/bash
pytest \
--cov=rpy2.robjects.lib \
rpy2/tests/robjects/lib
and it hangs after printing
============================================= test session starts ==============================================
platform win32 -- Python 3.9.7, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\tikum\Documents\Python\rpy2
plugins: cov-3.0.0
collecting ...
Debugging the pytest execution, I can follow up until it reaches coverage.py's Collector._start_tracer() (collector.py Line 278):
fn = tracer.start()
I basically run out of ideas at this point on what to do. Can anyone point me in the right direction on what to look for to resolve this issue?
Versions:
coverage 6.4.1
pytest 7.1.2
pytest-cov 3.0.0
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 .
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'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.