pytest

parameterized test with cartesian product of arguments in pytest

安稳与你 提交于 2019-11-28 21:03:15
问题 Just wondering, is there any (more) elegant way of parameterizing with the cartesian product? This is what I figured out so far: numbers = [1,2,3,4,5] vowels = ['a','e','i','o','u'] consonants = ['x','y','z'] cartesian = [elem for elem in itertools.product(*[numbers,vowels,consonants])] @pytest.fixture(params=cartesian) def someparams(request): return request.param def test_something(someparams): pass At least I'd like to encapsulate numbers, vowels, consonants and cartesian in the fixture

How to test single file under pytest

☆樱花仙子☆ 提交于 2019-11-28 20:47:58
问题 How do you test a single file in pytest? I could only find ignore options and no "test this file only" option in the docs. Preferably this would work on the command line instead of setup.cfg , as I would like to run different file tests in the ide. The entire suite takes too long. 回答1: simply run pytest with the path to the file something like pytest tests/unit/some_test_file.py 回答2: This is pretty simple: $ pytest -v /path/to/test_file.py The -v flag is to increase verbosity. If you want to

pytest--fixture

。_饼干妹妹 提交于 2019-11-28 20:35:49
前戏 fixture是在测试函数运行前后,由pytest执行的外壳函数。fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集、配置测试前系统的初始状态、为批量测试提供数据源等等。 下面是一个简单的fixture import pytest @pytest.fixture() def some_data(): return 37 def test_some_data(some_data): assert some_data == 37 我们来运行一下 @pytest.fixture()装饰器用于声明函数是一个fixture,如果测试函数的参数列表中包含fixture名,那么pytest会检测到,并在测试函数运行之前执行该fixture。fixture可以完成任务,也可以返回数据给测试函数。 测试用例test_some_data()的参数列表中包含一个fixture名some_data,pytest会以该名称搜索fixture。pytest会优先搜索该测试所在的模块,然后搜索conftest.py 建议:被fixture装饰的函数别以test命名,比如上面的命名为some_data,而不是test_data。当然,这样命名也没有错,只是为了方便区分是fixture还是测试用例(函数) conftest.py fixture可以放在单独的测试文件里

Using py.test with coverage doesn't include imports

那年仲夏 提交于 2019-11-28 20:06:09
For Jedi we want to generate our test coverage . There is a related question in stackoverflow, but it didn't help. We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example __init__.py is always reported as being uncovered: Name Stmts Miss Cover -------------------------------------------------- jedi/__init__ 5 5 0% [..] Clearly this file is being imported and should therefore be reported as tested. We start tests like this [*]: py.test --cov jedi As you can see we're using pytest-coverage . So how is it possible to

Test discovery failure when tests in different directories are called the same

ε祈祈猫儿з 提交于 2019-11-28 18:31:26
Using py.test, two tests called the same in different directory causes py.test to fail. Why is that? How can I change this without renaming all the tests? To duplicate do: ; cd /var/tmp/my_test_module ; mkdir -p ook/test ; mkdir -p eek/test ; touch ook/test/test_proxy.py ; touch eek/test/test_proxy.py ; py.test ============================= test session starts ============================== platform linux2 -- Python 2.7.3 -- pytest-2.2.4 collected 0 items / 1 errors ==================================== ERRORS ==================================== ___________________ ERROR collecting ook/test

How do I correctly setup and teardown my pytest class with tests?

萝らか妹 提交于 2019-11-28 17:22:46
I am using selenium for end to end testing and I can't get how to use setup_class and teardown_class methods. I need to set up browser in setup_class method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_clas s method. But logically it seems bad solution, because in fact my tests will work not with class, but with object. I pass self param inside every test method, so I can access objects' vars: class TestClass: def setup_class(cls): pass def test_buttons(self, data): # self.$attribute can be used, but not cls.$attribute? pass def test_buttons2

Ensuring py.test includes the application directory in sys.path

你离开我真会死。 提交于 2019-11-28 16:50:18
I have a project directory structure as follows (which I think is pretty standard): my_project setup.py mypkg __init__.py foo.py tests functional test_f1.py unit test_u1.py I'm using py.test for my testing framework, and I'd expect to be able to run py.test tests when in the my_project directory to run my tests. This does indeed work, until I try to import my application code using (for example) import mypkg in a test. At that point, I get the error "No module named mypkg". On doing a bit of investigation, it appears that py.test runs the tests with the directory of the test file in sys.path ,

How to suppress py.test internal deprecation warnings

走远了吗. 提交于 2019-11-28 16:34:42
问题 Is there a way to suppress the pytest's internal deprecation warnings? Context: I'm looking to evaluate the difficulty of porting a test suite from nose to pytest . The suite is fairly large and heavily uses nose -style yield based test generators. I'd like to first make sure the existing tests pass with pytest, and then maybe change test generators to parameterized . Just running $ pytest path-to-test-folder with pytest 3.0.4 is completely dominated by pages and pages of WC1 ~repos/numpy

你会写单元测试吗

耗尽温柔 提交于 2019-11-28 16:07:01
关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android、Python、Java和Go,这个也是我们团队的主要技术栈。 Github: https://github.com/hylinux1024 微信公众号:终身开发者(angrycode) 也许你已经听说过 Test Driven Development ,但不知道你是否遵循这个规则呢?其实我自己在写代码的时候也很少会先写单元测试再写业务功能逻辑。这不我也今天也来学习如何在 Python 中写单元测试。 0x00 unittest Python 中的 unittest 单元测试框架跟其它语言如 JUnit 是类似的。它支持测试自动化、配置共享以及关机代码测试。 假设在我的项目目录下有一个 mysum 模块用于计算列表中各个数之和。 还有一个 test_mysum.py 用于编写单元测试的文件。 myproject/ │ ├── mysum/ │ └── __init__.py └── unittests └── test_mysum.py 打开 mysum 模块中的 __init__.py 文件。 添加下面的方法 def sum(args): total = 0 for arg in args: total += arg return total 打开 test_mysum

How to print to console in pytest?

余生颓废 提交于 2019-11-28 15:25:44
问题 I'm trying to use TDD (test-driven development) with pytest . pytest will not print to the console when I use print . I am using pytest my_tests.py to run it. The documentation seems to say that it should work by default: http://pytest.org/latest/capture.html But: import myapplication as tum class TestBlogger: @classmethod def setup_class(self): self.user = "alice" self.b = tum.Blogger(self.user) print "This should be printed, but it won't be!" def test_inherit(self): assert issubclass(tum