pytest

py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: when I am trying to run my test through command line py.test file_name.py I got this error: py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config How can I fix this? 回答1: pytest-cov package is required if you want to pass --cov arguments to pytest, by default it should not be passed though. Are you using a modified version of py.test? pip install pytest-cov would fix your issue. 回答2: For those who use CentOS 6, the version of setuptools is old and you need to upgrade it also: pip install pytest-cov

How do I force pytest to write color output?

↘锁芯ラ 提交于 2019-12-03 08:16:52
问题 How do I force pytest to show the results in color, even when writing to a pipe? There does not seem to be any command line option to do so. 回答1: As of 2.5.0 py.test has the option --color=yes As of 2.7.0, it should be also possible to do: export PYTEST_ADDOPTS="--color=yes" 回答2: The "py" library that pytest uses will not use color if it doesn't detect a terminal. I ended up making a change to it to allow setting an enviroment variable ( PY_FORCE_COLOR=1 ) to force the color. I submitted a PR

Can I pass arguments to pytest fixtures?

妖精的绣舞 提交于 2019-12-03 08:15:44
The baseline of all my tests is that there will always be a taxi with at least one passenger in it. I can easily achieve this setup with some basic fixtures: from blah import Passenger, Taxi @pytest.fixture def passenger(): return Passenger() @pytest.fixture def taxi(passenger): return Taxi(rear_seat=passenger) Testing the baseline is straightforward: def test_taxi_contains_passenger(taxi) assert taxi.has_passenger() My issue crops up when I start needing more complicated test setup. There will be scenarios where I'll need the taxi to have more than one passenger and scenarios where I'll need

Can PyCharm drop into debug when py.test tests fail

筅森魡賤 提交于 2019-12-03 07:34:59
问题 When running tests with py.test there is a --pdb option to enter pdb on failure. Is there a similar way to enter the debugger when running the same test from within PyCharm? 回答1: There is a py.test plugin, pytest-pycharm, that will halt the PyCharm debugger when a test emits an uncaught exception. 回答2: Follow the steps below to setup the correct run configuration: Run > Edit Configurations... Click the '+' button to add a new configuration. Name the configuration and specify the configuration

Creating databases in SQLAlchemy tests with PostgreSQL

一曲冷凌霜 提交于 2019-12-03 07:15:17
I am building a Pyramid web application which is built on the top of SQLAlchemy and solely relies PostgreSQL as its database backend. What would be a way to have the unit tests structure so that Database is built once per test run - not on every test setUp() as this is too slow for a complex application Database tables are (re)created as they would be created in production (e.g. run migrations from Alembic). Any unclean databases are destroyed at the start of the test run. It is possible to choose a custom test runner á la py.test if a specific features outside the standard library unittest

How to keep track of instances of python objects in a reliable way?

ⅰ亾dé卋堺 提交于 2019-12-03 06:57:20
I would like to be able to keep track of instances of geometric Point objects in order to know what names are already "taken" when automatically naming a new one. For instance, if Points named "A", "B" and "C" have been created, then the next automatically named Point is named "D". If Point named "D" gets deleted, or its reference gets lost, then name "D" becomes available again. The main attributes of my Point objects are defined as properties and are the quite standard x , y and name . Solution with a problem and a "heavy" workaround I proceeded as described here , using a weakref.WeakSet()

How to test a Django model with pytest?

一笑奈何 提交于 2019-12-03 06:33:32
I am getting started with pytest. I have configured pytest, anyway I couldn't found a resource on Django specific testing with pytest. How do I test a model with pytest_django ? I have already asked a question on unittesting, how do I efficiently test this Django model? I want know how the same tests can be written with py.test? adding below the model and the tests written in unittest. the model under test is, class User(AbstractBaseUser, PermissionsMixin): username = models.CharField(max_length=25, unique=True, error_messages={ 'unique': 'The username is taken' }) first_name = models

Unable to debug in pycharm with pytest

核能气质少年 提交于 2019-12-03 06:32:49
问题 I cannot debug in PyCharm using py.test. All the test suite is running ok in "Debug mode" but it doesn't stop on breakpoints. I also have py.test as the default test runner. Maybe this is not important, but debugging works correctly in my Django server. Any ideas? picture of enable_breakpoints_and_the_mode_of_pycharm_is_debug References: pycharm-enabling-disabling-and-removing-breakpoints Run/Debug Configuration: py.test 回答1: For my situation, i found what the problem is: If there is --cov in

“py.test” vs “pytest” command

邮差的信 提交于 2019-12-03 06:28:31
问题 The py.test command is failing in my case, whereas pytest is running totally fine. I use the pytest-flask plugin: platform linux -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1 rootdir: /home/sebastian/develop/py/flask-rest-template, inifile: plugins: flask-0.10.0 When I invoke $ py.test I get the following error: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 301, in _getconftestmodules return self._path2confmods[path] KeyError

Can I run line_profiler over a pytest test?

江枫思渺然 提交于 2019-12-03 04:33:53
I have identified some long running pytest tests with py.test --durations=10 I would like to instrument one of those tests now with something like line_profiler or cprofile. I really want to get the profile data from the test itself as the pytest setup or tear down could well be part of what is slow. However given how line_profiler or cprofile is typically involved it isn't clear to me how to make them work with pytest. Run pytest like this: python -m cProfile -o profile $(which py.test) You can even pass in optional arguments: python -m cProfile -o profile $(which py.test) \ tests/worker/test