pytest

Isolating py.test DB sessions in Flask-SQLAlchemy

人盡茶涼 提交于 2019-11-29 01:33:05
问题 I'm trying to build a Flask app with Flask-SQLAlchemy; I use pytest to test the DB. One of the problems seems to be creating isolated DB sessions between different tests. I cooked up a minimal, complete example to highlight the problem, note that test_user_schema1() and test_user_schema2() are the same. Filename: test_db.py from models import User def test_user_schema1(session): person_name = 'Fran Clan' uu = User(name=person_name) session.add(uu) session.commit() assert uu.id==1 assert uu

Create and import helper functions in tests without creating packages in test directory using py.test

丶灬走出姿态 提交于 2019-11-29 01:32:06
问题 Question How can I import helper functions in test files without creating packages in the test directory? Context I'd like to create a test helper function that I can import in several tests. Say, something like this: # In common_file.py def assert_a_general_property_between(x, y): # test a specific relationship between x and y assert ... # In test/my_test.py def test_something_with(x): some_value = some_function_of_(x) assert_a_general_property_between(x, some_value) Using Python 3.5, with

Printing test execution times and pinning down slow tests with py.test

故事扮演 提交于 2019-11-29 01:21:23
问题 I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally. Is there a way to make py.test print out execution times of (slow) test, so pinning down problematic tests become easier? 回答1: I'm not sure this will solve your problem, but you can pass --durations=N to print the slowest N tests after the test suite finishes. 回答2: You can pass the

Py.test No module named *

我们两清 提交于 2019-11-29 01:06:14
I have a folder structure like this App --App --app.py --Docs --Tests --test_app.py In my test_app.py file , I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How should I configure this? So you are running py.test from /App . Are you sure /App/App is in your $PYTHONPATH ? If it's not, code that tries to import app will fail with such a message. EDIT0: including the info from my comment below, for completeness. An attempt to import app will only succeed if it was executed inside /App/App , which is not the case here. You

初识pytest

老子叫甜甜 提交于 2019-11-29 00:30:16
pytest是python的一种单元测试框架(非自带,需要安装),与python自带的unitest测试框架相比,使用起来更加简洁、效率更高 来源: https://www.cnblogs.com/wang-mengmeng/p/11435610.html

pytest using fixtures as arguments in parametrize

狂风中的少年 提交于 2019-11-28 23:25:16
I would like to use fixtures as arguments of pytest.mark.parametrize or something that would have the same results. For example: import pytest import my_package @pytest.fixture def dir1_fixture(): return '/dir1' @pytest.fixture def dir2_fixture(): return '/dir2' @pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')] def test_directory_command(dirname, expected): result = my_package.directory_command(dirname) assert result == expected The problem with fixture params is that every param of the fixture will get run every time it's used, but I don't

Can I perform multiple assertions in pytest?

浪子不回头ぞ 提交于 2019-11-28 23:07:22
问题 I'm using pytest for my selenium tests and wanted to know if it's possible to have multiple assertions in a single test? I call a function that compares multiple values and I want the test to report on all the values that don't match up. The problem I'm having is that using "assert" or "pytest.fail" stops the test as soon as it finds a value that doesn't match up. Is there a way to make the test carry on running and report on all values that don't match? 回答1: As Jon Clements commented, you

pytest-conftest.py作用范围

一世执手 提交于 2019-11-28 23:00:22
1.目录结构 调用fixture有两种写法: 1.装饰器 @pytest.mark.usefixtures("start") ; 2.直接在用例里面调用fixture装饰的方法当作参数输入def test_demo(self,start);3.设置fixture参数autouse=True 2.用例传fixture参数 1.项目跟目录下面的全局conftest.py 1 import pytest 2 @pytest.fixture() 3 def start(): 4 print("\n全局conftest") 2.test_case_demo/conftest.py 和 test_demo.py conftest.py 代码1 import pytest 2 @pytest.fixture() 3 def start_1(): 4 print("\n局部test_case_demo") test_demo.py 代码1 import pytest 2 class Test_demo(): 3 def test_demo(self,start,start_1): #标记代码 4 assert 1==1 5 6 if __name__ == '__main__': 7 pytest.main(["-s","-v","test_demo.py"]) 运行结果: 3.test_case

PyCharm noinspection for whole file?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 22:32:24
问题 Is it possible to disable an inspection for the whole file in PyCharm? The reason this is needed is when dealing with py.test. It uses fixtures which appear to shadow function parameters, and at the same time cause unresolved references. e.g.: from myfixtures import user # Unused import statement warning def test_is_awesome(user): # Shadows name 'user' from outer scope warning assert user.is_awesome() There is also other warnings from py.test, such as using pytest.raises() causes a "Can not