pytest

py.test logging messages and test results/assertions into a single file

时光毁灭记忆、已成空白 提交于 2019-12-23 18:28:26
问题 I am starting to work with py.test at the moment for a new project. We are provisioning Linux servers and I need to write a script to check the setup and configuration of these servers. I thought that py.test is a good way to implement these tests and it is working quite fine until now. The problem I face right now is that I need a log file at the end of these tests showing some log messages for each test and the result of the test. For the log messages I use logger: logging.basicConfig

ModuleNotFoundError issue for pytest

回眸只為那壹抹淺笑 提交于 2019-12-23 17:43:23
问题 I want to use pytest to do unit testing for the scripts in another folder called src . Here is my directory structure: src __init__.py script1.py script2.py test test_fun.py However, when I try to run pytest in the test folder through command line, I saw the following error from script2 import fun2 E ModuleNotFoundError: No module named 'script2' In each script, I have the following contents script2.py : def fun2(): return('result_from_2') script1.py : from script2 import fun2 def fun1():

Parametrizing test with multiple fixtures that accept arguments [duplicate]

浪子不回头ぞ 提交于 2019-12-23 13:04:07
问题 This question already has answers here : How to concatenate several parametrized fixtures into a new fixture in py.test? (3 answers) Closed last year . I am trying to test a math function that I wrote. I would like to supply data to it from a number of different fixtures. The issue is that all of the fixtures accept different fixture parameters of their own. The test that I run is always the same ( test_myfunc in the example), and the fixtures that I want to plug into it all have the same

Is there a way to override default assert in pytest (python)?

痞子三分冷 提交于 2019-12-23 12:08:03
问题 I'd like to a log some information to a file/database every time assert is invoked. Is there a way to override assert or register some sort of callback function to do this, every time assert is invoked? Regards Sharad 回答1: I don't think that would be possible. assert is a statement (and not a function) in Python and has a predefined behavior. It's a language element and cannot just be modified. Changing the language cannot be the solution to a problem. Problem has to be solved using what is

Pytest - how to skip tests unless you declare an option/flag?

大憨熊 提交于 2019-12-23 09:26:21
问题 I have some unit tests, but I'm looking for a way to tag some specific unit tests to have them skipped unless you declare an option when you call the tests. Example: If I call pytest test_reports.py , I'd want a couple specific unit tests to not be run. But if I call pytest -<something> test_reports , then I want all my tests to be run. I looked into the @pytest.mark.skipif(condition) tag but couldn't quite figure it out, so not sure if I'm on the right track or not. Any guidance here would

Check if any tests raise a deprecation warning with pytest

杀马特。学长 韩版系。学妹 提交于 2019-12-23 09:15:44
问题 I am using pytest to run tests in a Python package, and I would like to know if any of the code that is executed as part of the tests is raising deprecation warnings (when all tests are passing). Does anyone know of a way to do this? 回答1: The code import warnings warnings.simplefilter("error") will turn (all) warnings into errors, which may help. Alternatively, you can get a list of generated warnings with import warnings with warnings.catch_warnings(record=True) as w: warnings.warn(

PytestDeprecationWarning at test setup: the funcargnames attribute was an alias for fixturenames

喜欢而已 提交于 2019-12-23 09:00:07
问题 Following tutorial on https://testdriven.io/, I have created a websocket test for testing my websocket connection: # tests/test_websockets.py from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.test import Client from channels.db import database_sync_to_async from channels.layers import get_channel_layer from channels.testing import WebsocketCommunicator from nose.tools import assert_equal, assert_is_none, assert_is_not_none, assert_true

pytest-mock mocker in pytest fixture

点点圈 提交于 2019-12-23 08:48:01
问题 I'm trying to find out why I don't seem to be able to use a mocked return value in a fixture. With the following imports import pytest import uuid pytest-mock example that works: def test_mockers(mocker): mock_uuid = mocker.patch.object(uuid, 'uuid4', autospec=True) mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f') # this would return a different value if this wasn't the case assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f' The above test passes. However

Setting command line arguments for main function tests

纵饮孤独 提交于 2019-12-23 08:24:35
问题 I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code? e.g. def main(): # argparse code args, other = arg_parser.parse_known_args() return args.first_arg def test_main(): res = main() # call with first_arg = "abc" assert(res == "abc") 回答1: parse_args takes a argv parameter. The docs uses this repeatedly in it's examples parser = argparse.ArgumentParser() parser.add_argument('--foo

Setting command line arguments for main function tests

两盒软妹~` 提交于 2019-12-23 08:24:11
问题 I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code? e.g. def main(): # argparse code args, other = arg_parser.parse_known_args() return args.first_arg def test_main(): res = main() # call with first_arg = "abc" assert(res == "abc") 回答1: parse_args takes a argv parameter. The docs uses this repeatedly in it's examples parser = argparse.ArgumentParser() parser.add_argument('--foo