pylint gives error when run by tox, but not otherwise - python

I have been adding pylint and tox to a simple project: https://github.com/rrthomas/rpl/tree/tox
When I run pylint on my code, with some warnings turned off, I get no errors:
$ pylint --disable=C,fixme,too-many-locals,too-many-branches,too-many-statements rpl
-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 9.68/10, +0.32)
However, when I run the same pylint command with tox, it gives me errors:
$ tox -e py310
...
py310 run-test: commands[2] | pylint --disable=C,fixme,too-many-locals,too-many-branches,too-many-statements rpl
************* Module rpl
rpl:356:11: E1101: Module 'regex' has no 'MULTILINE' member (no-member)
rpl:356:30: E1101: Module 'regex' has no 'IGNORECASE' member (no-member)
------------------------------------------------------------------
Your code has been rated at 9.68/10 (previous run: 9.68/10, +0.00)
I'm using the identical version of pylint both with and without tox.
I'm baffled, and I don't know whether I've done something wrong, or whether there's some problem, in which case, whose fault is it?

tox runs the tests in isolation. I'd guess tox is not moving the pylint configuration file so the pylint configuration is not automatically detected anymore by pylint. Try using the --rcfile option to specify the configuration file.

Add the --verbose flag to the pylint command which is run by tox. Then it tells you which configuration file is actually being used during the execution. The result is often rather unexpected.
I've written a bit more about the background here (albeit in a somewhat different context) .

Related

Is Python 3.9.6 compatible with pytest 6.2.5?

I am trying to test views and models for Django REST API written in pycharm and have installed pytest for this. I have written some tests and when I wanted to start them I got the following error message:
ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] [...]
_jb_pytest_runner.py: error: unrecognized arguments: --cov=frontend --cov-report=html
I have then checked if I have installed pytest properly and it seems I have. I have both Python 2.7.16 as well as Python 3.9.6 installed but am using Python 3. Could this be a compatibility problem or is it something else?
I have tried starting the tests both through the terminal using py.test and just in the IDE itself. I keep getting this same error message.
I have tried the approach below:
py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config
yet I seem to get the same error.
ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] [...]
_jb_pytest_runner.py: error: unrecognized arguments: --cov=frontend --cov-report=html
Does anyone know how I could solve this issue?
Thanks in advance.
First of all, yes, Python 3.9.6 is compatible with pytest 6.2.5, however, you appear to be missing a few dependencies. pytest is one of many different Python packages, and you appear to have installed that successfully, so you're halfway there.
There are a few different coverage plugins that work with pytest, and those need to be installed separately. Here are the two most common coverage plugins for Python and pytest:
https://pypi.org/project/coverage/
https://pypi.org/project/pytest-cov/
The first one, coverage is installed with:
pip install coverage
The second one, pytest-cov is installed with:
pip install pytest-cov
Based on your run command, you appear to want to use pytest-cov. After you've installed that, you can verify that pytest has those new options by calling pytest --help:
> pytest --help
...
coverage reporting with distributed testing support:
--cov=[SOURCE] Path or package name to measure during execution (multi-allowed). Use --cov= to
not do any source filtering and record everything.
--cov-reset Reset cov sources accumulated in options so far.
--cov-report=TYPE Type of report to generate: term, term-missing, annotate, html, xml (multi-
allowed). term, term-missing may be followed by ":skip-covered". annotate, html
and xml may be followed by ":DEST" where DEST specifies the output location.
Use --cov-report= to not generate any output.
--cov-config=PATH Config file for coverage. Default: .coveragerc
--no-cov-on-fail Do not report coverage if test run fails. Default: False
--no-cov Disable coverage report completely (useful for debuggers). Default: False
--cov-fail-under=MIN Fail if the total coverage is less than MIN.
...
Alternatively, you might be able to get the same results you're looking for using coverage:
coverage run -m pytest
coverage html
coverage report
And that will also give you a coverage report even if not using pytest-cov options.

vscode python extension displayDiscoverStatus [SyntaxError: Unexpected end of JSON input

Code gives me the following errors when pytest performs test discovery. I'm using a virtual environment (Pipenv) and have the environment activated in vscode. The error occurred some time after i upgraded to vscode Juli version 2020. I have tried removing and purging code with dpkg, and deleted the extensions located in ~/.vscode/extensions. After that, I installed the June version where i previously had no errors, but the error still occurs.
Python test log:
python /home/christian/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/christian/Documents/source/personalprojects/portfolioapp/services/activity-service -s --cache-clear tests
Test Discovery failed:
SyntaxError: Unexpected end of JSON input
Python log:
> ~/.local/share/virtualenvs/activity-service-aBZdkeOh/bin/python ~/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/Documents/source/personalprojects/portfolioapp/services/activity-service -s --cache-clear tests
cwd: ~/Documents/source/personalprojects/portfolioapp/services/activity-service
Error 2020-08-16 17:51:49: Failed to parse discovered Test [SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at /home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:39:327177
at f.exec (/home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:48:935503)
at async f.discoverTests (/home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:48:934976)] {
stdout: ''
}
Error 2020-08-16 17:51:49: Python Extension: displayDiscoverStatus [SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at /home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:39:327177
at f.exec (/home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:48:935503)
at async f.discoverTests (/home/christian/.vscode/extensions/ms-python.python-2020.8.101144/out/client/extension.js:48:934976)] {
stdout: ''
}
If I run pytest inside the virtual env, The test runs succesfully without errors - so it seems have something to do with the python extension.
Does anyone know how to fix this?
The error message indicates the output of the pytest discover test command is not conformed to JSON regulation.
And you said the pytest works well in the terminal. So could you run the command in the Python test log directly in the terminal, and to check the outputs of this command?
the command:
python /home/christian/.vscode/extensions/ms-python.python-2020.8.101144/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/christian/Documents/source/personalprojects/portfolioapp/services/activity-service -s --cache-clear tests
If the outputs of this command really does not comply with JSON regulation that means the problem was caused by the pytest package, you need to fix the pytest package you are using.
If the outputs comply with the JSON regulation that means the problem was caused by the Python extension or the VSCode, as you have deleted the extension be located at ~/.vscode/extensions and reinstall the VSCode but the error still exists, so could you delete the project cache which under: C:\Users[USERNAME]\AppData\Roaming\Code\User\workspaceStorage(in windows)?

How to disable PytestDeprecationWarning: direct construction of Flake8Item has been deprecated, please use Flake8Item.from_parent

When I run python setup.py test command in my project folder to test my package with setup.cfg configuration I got this warning message.
How can I disable it?
python setup.py test acts like pytest --flake8 command.
============================================================================================== warnings summary ===============================================================================================
c:\users\yedhrab\appdata\local\programs\python\python38\lib\site-packages\pytest_flake8.py:65
c:\users\yedhrab\appdata\local\programs\python\python38\lib\site-packages\pytest_flake8.py:65: PytestDeprecationWarning: direct construction of Flake8Item has been deprecated, please use Flake8Item.from_parent
return Flake8Item(
-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================================================================================== short test summary info ===========================================================================================
SKIPPED [1] c:\users\***\appdata\local\programs\python\python38\lib\site-packages\pytest_flake8.py:106: file(s) previously passed FLAKE8 checks
=================================================================================== 9 passed, 1 skipped, 1 warning in 0.33s ===================================================================================
If you do not want to disable all warnings but only a specific pytest warning, use the -W parameter:
pytest -W ignore::pytest.PytestDeprecationWarning
You should use the --disable-warnings flag to silence the warnings for a particular test if that's really what you want.
pytest --disable-warnings
See for more details by running pytest --help.
I solved this issue via adding these lines to my setup.cfg
[tool:pytest]
# ...
filterwarnings =
ignore::DeprecationWarning

PyTest deprecation: 'junit_family default value will change to 'xunit2'

I'm getting deprecation warning from my pipelines at circleci.
Message.
/home/circleci/evobench/env/lib/python3.7/site-packages/_pytest/junitxml.py:436: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0.
Command
- run:
name: Tests
command: |
. env/bin/activate
mkdir test-reports
python -m pytest --junitxml=test-reports/junit.xml
How should I modify command to use xunit?
Is it possible to a default tool, as it is mentioned in the message?
I mean without specyfing xunit or junit.
Here's full pipeline.
Run your command in this ways.
with xunit2
python -m pytest -o junit_family=xunit2 --junitxml=test-reports/junit.xml
with xunit1
python -m pytest -o junit_family=xunit1 --junitxml=test-reports/junit.xml or
python -m pytest -o junit_family=legacy --junitxml=test-reports/junit.xml
This here describes the change in detail:
The default value of junit_family option will change to xunit2 in
pytest 6.0, given that this is the version supported by default in
modern tools that manipulate this type of file.
In order to smooth the transition, pytest will issue a warning in case
the --junitxml option is given in the command line but junit_family is
not explicitly configured in pytest.ini:
PytestDeprecationWarning: The `junit_family` default value will change to 'xunit2' in pytest 6.0. Add `junit_family=legacy` to your
pytest.ini file to silence this warning and make your suite
compatible.
In order to silence this warning, users just need to configure the
junit_family option explicitly:
[pytest]
junit_family=legacy
In your pytest.ini file add the following line:
junit_family=legacy
If you want to keep the default behavior of the --junitxml option. Or you can accept the new version, xunit2 but not explicitly defining the junit_family variable.
Essentially what the warning is saying is you are giving the --junitxml option in your
run
name: Tests
section not specifying the junit_family variable. You need to start to explicitly defining it to remove the warning or accept the new default.
This thread goes into more details about where to find the .ini file for pytest.
For official statement/documentation about moving from xunit1 to xunit2 read: docs.pytest.org
Also if your project contains pytest.ini file you can set junit_family usage directly from the file like:
# pytest.ini
[pytest]
minversion = 6.0
junit_family=xunit2
junit_suite_name = Pytest Tests
addopts = -ra -q -v -s --junitxml=path/to/pytest_results/pytest.xml
The other answers pretty much covered the means of specifying the junit family, either within pytest.ini or at the commandline with an ini option override.
It's worth looking at the differences between xunit1 and xunit2 for a concrete xml file. Doing a quick spot check on the differences I found these differences show in the image below for the following test module...
# test_stub.py
import sys
import pytest
def test_pass():
assert True
def test_fail():
assert False
if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
Pytest was ran under three separate configurations (which match the vertical order in the image)
# Default execution
pytest test_stub.py --junit-xml=out.xml
pytest test_stub.py --junit-xml=out_xunit2.xml -o junit_family=xunit2
# Junit prefix execution
pytest test_stub.py --junit-prefix=FOOOP --junit-xml=out_prefix.xml
pytest test_stub.py --junit-prefix=FOOOP --junit-xml=out_prefix_xunit2.xml -o junit_family=xunit2
# Junit suite execution
pytest -o junit_suite_name=SUITE test_stub.py --junit-xml=out_suite.xml
pytest -o junit_suite_name=SUITE test_stub.py --junit-xml=out_suite_xunit2.xml -o junit_family=xunit2
All of the diffs pretty much highlight the fact that xunit2 omits the file and line attributes that showed up previously in xunit1. The other diffs were merely timestamp differences. Both junit_suite_name and junit-prefix behave as before.
Another major difference is that for some reason record_property has been deprecated under the xunit2 schema.
PytestWarning: record_property is incompatible with junit_family 'xunit2' (use 'legacy' or 'xunit1')
https://docs.pytest.org/en/7.1.x/reference/reference.html#pytest.junitxml.record_property

Does pytest plugins work with python2.6

I am new to pytest and I have python2.6 installed on my setup.
I installed pytest and the testcases get executed properly. I installed couple of plugins like pytest-timeout, putest-xdist etc but these plugins does not load when I run the cases. For timeout, I get following error: py.test: error: unrecognized arguments: --timeout
Same steps followed with python2.7 works.
Any idea how this can be solved or alteast steps to debug to know what exactly is causing the issue.
Unfortunately pytest < 3.0 "hides" the ImportError happening when failing to import a plugin. If you remove all plugin arguments but add -rw, you should be able to see what exactly is going wrong in the warning summary.
In your conftest.py file just add the following line after the imports:
pytest_plugins = 'pytest_timeout'
It should solve your issue.

Categories