pytest

Does pytest parametrized test work with unittest class based tests?

筅森魡賤 提交于 2019-12-03 04:25:44
I've been trying to add parametrized @pytest.mark.parametrize tests to a class based unittest. class SomethingTests(unittest.TestCase): @pytest.mark.parametrize(('one', 'two'), [ (1, 2), (2, 3)]) def test_default_values(self, one, two): assert one == (two + 1) But the parametrized stuff didn't kick in : TypeError: test_default_values() takes exactly 3 arguments (1 given) I've switched to simple class based tests (without unittest). But I'd like to know if anyone tried it and it worked. According to pytest documentation : unittest.TestCase methods cannot directly receive fixture function

Pytest: Deselecting tests

跟風遠走 提交于 2019-12-03 03:31:12
问题 With pytest, one can mark tests using a decorator @pytest.mark.slow def some_slow_test(): pass Then, from the command line, one can tell pytest to skip the tests marked "slow" pytest -k-slow If I have an additional tag: @pytest.mark.long def some_long_test() pass I would like to be able to skip both long AND slow tests. I've tried this: pytest -k-slow -k-long and this: pytest -k-slow,long And neither seems to work. At the command line, how do I tell pytest to skip both the slow AND the long

How can I use a parametrized dependent fixture twice in pytest?

笑着哭i 提交于 2019-12-03 03:12:09
I'm trying to use a parametrized fixture multiple times in a single test, with the intent of getting a cartesian product of all of its values. https://stackoverflow.com/a/39444098/102441 shows how to do this for a simple fixture: import pytest @pytest.fixture(params=[0, 1, 2]) def first(request): return request.param second = first # runs 3x3 = 9 times def test_double_fixture(first, second): assert False, '{} {}'.format(first, second) However, this approach falls apart if the parametrization comes from a dependent fixture: import pytest @pytest.fixture(params=[0, 1, 2]) def integer(request):

Test case execution order in pytest

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order? 回答1: In general you can configure the behavior of basically any part of pytest using its well-specified hooks . In your case, you want the "pytest_collection_modifyitems" hook,

pytest cannot import module while python can

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am working on a package in Python. I use virtualenv. I set the path to the root of the module in a .pth path in my virtualenv, so that I can import modules of the package while developing the code and do testing (Question 1: is it a good way to do?). This works fine (here is an example, this is the behavior I want): ( VEnvTestRc ) zz@zz :~ /Desktop/ GitFolders / rc$ python Python 2.7 . 12 ( default , Jul 1 2016 , 15 : 12 : 24 ) [ GCC 5.4 . 0 20160609 ] on linux2 Type "help" , "copyright" , "credits" or "license" for more

Does pytest parametrized test work with unittest class based tests?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been trying to add parametrized @pytest.mark.parametrize tests to a class based unittest. class SomethingTests(unittest.TestCase): @pytest.mark.parametrize(('one', 'two'), [ (1, 2), (2, 3)]) def test_default_values(self, one, two): assert one == (two + 1) But the parametrized stuff didn't kick in : TypeError: test_default_values() takes exactly 3 arguments (1 given) I've switched to simple class based tests (without unittest). But I'd like to know if anyone tried it and it worked. 回答1: According to pytest documentation : unittest

How to make pytest wait for (manual) user action?

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We are sucessfully using pytest (Python 3) to run a test suite testing some hardware devices (electronics). For a subset of these tests, we need the tester to change the hardware arrangement, and afterwards change it back. My approach was to use a module-level fixture attached to the tests in question (which are all in a separate module), with two input calls: @pytest.fixture(scope="module") def disconnect_component(): input('Disconnect component, then press enter') yield # At this point all the tests with this fixture are run input('Connect

How to pass environment variables to pytest

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Before i start executing the tests in my python project, i read some environment variables and set some variables with these values read. My tests will run on the desired environment based on these values read. for eg: Let's say the environment variables are called "ENV_NAME" and "ENV_NUMBER" Now, I would like to run the tests using py.test If i hard code these environment variables, for eg: ENV_NAME = 'staging', ENV_NUMBER = '5' in my code and then run the tests by executing the py.test command at the root of the project directory, all the

py.test logging control

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We have recently switched to py.test for python testing (which is fantastic btw). However, I'm trying to figure out how to control the log output (i.e. the built-in python logging module). We have pytest-capturelog installed and this works as expected and when we want to see logs we can pass --nologcapture option. However, how do you control the logging level (e.g. info, debug etc.) and also filter the logging (if you're only interested in a specific module). Is there existing plugins for py.test to achieve this or do we need to roll our own

Python - Use pytest to test test methods written in subclass

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am a novice to pytest. I have a scenario wherein i wanted to test some test methods written in a subclass. Assume that the following is my code structure class Superclass: def __init__(self, a): self.a = a def dummy_print(): print("This is a dummy function") class TestSubClass(Superclass): def test_1_eq_1(): assert 1 == 1 Upon executing the following command py.test -s -v test_filename.py I get the following error messgae: cannot collect test class 'Test_Super' because it has a init constructor The same is mentioned in the pytest