pytest

Missing “Code Coverage” tab on Python project

蹲街弑〆低调 提交于 2019-12-11 09:29:50
问题 I have a Python project hosted on GitHub, which I test using Azure Pipelines (here is a link to the configuration). I am running unit tests with pytest and get coverage results, which I upload in my pipeline. I get an overall code coverage percentage in the pipeline execution summary (example), but I don't see a "Code Coverage" tab that contains the details of which lines are covered. The build artifacts contain the HTML code coverage report, which contains this information, but it'd be nicer

Parametrize the test based on the list test-data from a json file

自作多情 提交于 2019-12-11 07:59:07
问题 Is there a way to parametrize a test, when test has a list of different/multiple test-data? example_test_data.json { "test_one" : [1,2,3], # this is the case, where the `test_one` test need to be parametrize. "test_two" : "split", "test_three" : {"three":3}, "test_four" : {"four":4}, "test_set_comparison" : "1234" } Directory structure: main -- conftest.py # conftest file for my fixtures testcases project_1 (contains these files -- test_suite_1.py, config.json) project_2 (contains these files

Repeat a test upon an AssertionError

廉价感情. 提交于 2019-12-11 07:08:17
问题 There is the module pytest-repeat which can be used to repeat the execution of pytest N times (the whole test suite). However, I want to, upon an AssertionError, re-run that specific test again, instead of running the whole suit. So, how can I capture the AssertionError and programmatically call the same test function? 回答1: The flaky package provides a @flaky decorator. From the documentation: @flaky(max_runs=3, min_passes=2) def test_something_that_usually_passes(self): """This test must

Testing whether a Url is giving 500 error or not in Django [duplicate]

早过忘川 提交于 2019-12-11 06:38:01
问题 This question already has answers here : Django's self.client.login(…) does not work in unit tests (6 answers) Closed 8 months ago . I want to test Urls whether am getting 500 error or not. In normal case where login is not required I get status_code 200 but where login is required, it gives me 302 error. So, how can one test loginrequired and paramterized url in best way. Thank You So I am adding this because someone link that question into duplicate but it is not my answer and why it is not

Mock exception with side effect raised in class method and caught in calling method gives 'did not raise'

断了今生、忘了曾经 提交于 2019-12-11 06:16:40
问题 Using side_effect, I am trying to raise an exception when a mock is called but I get a DID NOT RAISE EXCEPTION error that I do not understand. Based largely on this answer, I have created a simple example where there is a Query class with a class method make_request_and_get_response which can raise several exceptions. These exceptions are being handled within the get_response_from_external_api method in main.py . query.py from urllib.request import urlopen import contextlib import urllib

Wrap each pytest test function into try-except

落爺英雄遲暮 提交于 2019-12-11 05:59:43
问题 I want to wrap each of my test functions into a try-except block to execute code in the except block. This code should only be executed if the test is failing. I want to achieve this without altering the test functions, but instead use some kind of decorator/fixture. Unfortunately I can not find any examples. Example of what I'm trying to achieve: def test_1(): some_method_that_might_throw_an_exception() I have multiple tests and all of them should run a function run_only_if_exception_was

Gitlab CI variable, option with quotes

女生的网名这么多〃 提交于 2019-12-11 05:42:02
问题 I have the following gitab-ci.yml file : stages: - tests .test: &test_job image: name: test.com/test:latest entrypoint: [""] script: - py.test /test -v $EXTRA_OPTIONS testing: variables: EXTRA_OPTIONS: -m "not slow" <<: *test_job stage: tests I would like to pass option to run pytest like: py.test /tests -v -m "not slow" to avoid running slow tests, but gitlab is trying to escape quotes. I've got something like: py.test /tests -v -m '"not\' 'slow"' is it possible to create a variable that

Restricting py.test to only run pylint and not unittests

為{幸葍}努か 提交于 2019-12-11 04:47:51
问题 I'm trying to run py.test and execute only pylint, but not unittests. The documentation on this page indicates you can do it: https://pypi.org/project/pytest-pylint/ You can restrict your test run to only perform pylint checks and not any other tests by typing: py.test --pylint -m pylint But when I run that command exactly I still get errors from unittests that py.test found. The linting process seems to run as expected, then I get a bunch of errors in unittest files reported. This seems

Pytest fixtures interfering with each other

拜拜、爱过 提交于 2019-12-11 04:38:03
问题 I am using Pytest with Django and came to this weird behaviour. I have two user fixtures, one being a superset of the other. Everything works as expected until I use both fixtures in the same test case. Fixtures: @pytest.fixture def user_without_password(): return User.objects.create_user(username=fake.name(), email=fake.email()) @pytest.fixture def user_with_password(user_without_password): user = user_without_password user.set_password('topsecret') user.save() return user Tests @pytest.mark

Python package not being found on Pytest

我们两清 提交于 2019-12-11 04:29:15
问题 I have the following project structure: foo │ ├── foo │ ├── cli │ │ ├── tests │ │ └── __init__.py │ ├── core │ │ ├── tests │ │ └── __init__.py │ ├── tests │ │ ├── __init__.py │ │ └── test_foo.py │ ├── __init__.py │ └── foo.py ├── kube ├── requirements ├── ...any-other-non-source-related └── README.md Inside the foo/foo.py file I have the following placeholder code: import cli import core def say_hi(): print('hi') if __name__ == '__main__': say_hi() And on the file foo/tests/test_foo.py I have