pytest

Is pytest supposed to collect tests from dependency modules in a virtual environtment?

一个人想着一个人 提交于 2019-12-05 07:42:53
I am attempting to setup a project on another laptop than my typical development machine. This project has several pytest-based tests that I have written over the lifetime of the project. When I run $ pytest -k tests/my_test.py I get a list of errors from sqlalchemy tests like the following: _ ERROR collecting env/lib64/python3.5/site-packages/sqlalchemy/testing/suite/test_update_delete.py _ env/lib/python3.5/site-packages/py/_path/local.py:662: in pyimport __import__(modname) env/lib/python3.5/site-packages/sqlalchemy/testing/suite/__init__.py:2: in <module> from sqlalchemy.testing.suite.test

How to run tests in django using database with data?

我与影子孤独终老i 提交于 2019-12-05 07:19:26
I want to test my views using data from postgres localhost database (with already loaded data). I'm using tox with pytest and pytest-django. My question: How to set up / connect to local database to get all the data model schema and data itself? Or maybe it is better to use factory_boy? Or to load whole data from .sql script (if yes, how)? Example of my test: def test_foo_view(custom_client_login): response = custom_client_login.get('/foo/bar/123/') assert response.status_code == 200 assert 'Transaction no. 123' in response.content But instead of getting status code 200 I get 404, which points

pathlib Path and py.test LocalPath

大憨熊 提交于 2019-12-05 06:22:10
I have started using pathlib.Path some time ago and I like using it. Now that I have gotten used to it, I have gotten sloppy and forget to cast arguments to str . This often happens when using tox + py.test with temporary directories based on tmpdir (which is a py._path.local.LocalPath ): from pathlib import Path import pytest def test_tmpdir(tmpdir): p = Path(tmpdir) / 'testfile.csv' Instead of inserting str() every time, I looked at solving this more generally, but could not. First I tried to make my own Path class that has an adapted _parse_args : import pytest from py._path.local import

How to mock a imported object with pytest-mock or magicmock

孤人 提交于 2019-12-05 06:09:52
I am trying to understand the mock/monkeypatch/pytest-mock capabilities. Let me know if this is possible. If not could you please suggest how I can test this code. My code structure: / ./app ../__init__.py ../some_module1 .../__init__.py ../some_module2 .../__init__.py ./tests ../test_db.py The /app/__init__.py is where my application (a Flask application if it helps) is started along with initializing a database connection object to a MongoDB database: # ... def create_app(): # ... return app db_conn = DB() The some_module1 and some_module import the db_conn object and use it as part of their

django-pytest setup_method database issue

喜欢而已 提交于 2019-12-05 06:08:14
I have the following set up on Ubuntu 14.04: python 2.7.6 django 1.7 [though I reproduced the same behaviour with django 1.9 too] pytest-django 2.8.0 [also tested with 2.9.1] pytest 2.7.2 [also tested with 2.8.3] And the following test code: import pytest from django.db import connection import settings from pollsapp.models import Question original_db_name = settings.DATABASES["default"]["NAME"] @pytest.mark.django_db class TestExperiment(object): def setup_method(self, method): # it's not using the "test_" + DATABASE_NAME ! assert connection.settings_dict["NAME"] == \ settings.DATABASES[

With py.test, database is not reset after LiveServerTestCase

非 Y 不嫁゛ 提交于 2019-12-05 05:54:43
I have a number of Django tests and typically run them using py.test. I recently added a new test case in a new file test_selenium.py . This Test Case has uses the LiveServerTestCase and StaticLiveServerTestCase classes (which is a first for me, usually I am using just TestCase ). Adding this new batch of tests in this new file has caused subsequent tests to start failing in py.test (when before they all passed). It appears that the database is not being "reset" after the LiveServerTestCase in py.test. I can tell because of the incrementation of my model's pk values. When I run these tests

Layout and importing for pytest in python3

可紊 提交于 2019-12-05 03:55:52
I'm having trouble importing modules from my pytest functions. I know there's a million questions on this, but I've read through a bunch, and I'm still having trouble understanding. $ tree . └── code ├── eight_puzzle.py ├── missionaries_and_cannibals.py ├── node.py ├── search.py └── test ├── test_eight_puzzle.py └── test_search.py 2 directories, 6 files $ $ grep import code/test/test_search.py import sys import pytest import code.search $ $ pytest ... ImportError while importing test module '~/Documents/code/test/test_search.py'. Hint: make sure your test modules/packages have valid Python

Pytest plugin: Overriding pytest_runtest_call and friends

只谈情不闲聊 提交于 2019-12-05 03:55:12
I'm developing a test suite using pytest for a project of mine. Because of the nature of the project, I need to create a Pytest plugin that controls how the tests are being run; they are not run locally, but sent to a different process to run. (I know about xdist but I think it doesn't solve my problem.) I've been writing my own Pytest plugin by overriding the various pytest_runtest_* methods. So far it's been progressing well. Here is where I've hit a wall: I want my implementations of pytest_runtest_setup , pytest_runtest_call and pytest_runtest_teardown to actually be responsible for doing

python + pytest基本使用方法(断言)

本小妞迷上赌 提交于 2019-12-05 03:17:17
#pytest 的基本用法# 安装: pip install pytest#在当前目录下运行 : 输入 pytest# 1.断言#功能:用于计算a与b相加的和def add(a,b): return a + b#功能:用于判断素数def is_prime(n): if n < 1: return False for i in range(2,n): if n % i == 0: return False return True#测试相等def test_add_1(): assert add(3,4) == 7#测试不相等def test_add_2(): assert add(17,22) != 50#测试大于或者等于def test_add_3(): assert add(17,22) <= 50#测试小于或者等于def test_add_4(): assert add(17,22) >= 38#测试包含def test_in(): a = 'Hello' b = 'he' assert b in a#测试不包含def test_not_in(): a = 'Hello' b = 'hi' assert b not in a#判断是否为Truedef test_true_1(): assert is_prime(13)#判断是否为Truedef test_true_2():

python + pytest 基本使用方法(Fixture)

久未见 提交于 2019-12-05 03:17:08
#firtures通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境# setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行,只执行一次;# setup_function/teardown_function:在每个测试函数之前与之后执行;# setup/teardown:在每个测试函数之前与之后执行;# 在当前文件下打开cmd窗口执行:pytest -s test_fixtures_01.py#功能函数def multiply(a,b): return a * b#===============Firtures=============def setup_module(module): print('setup_module------------>')def teardown_module(module): print('teardown_module--------->')def setup_function(function): print('setup_function---------->')def teardown_function(function): print('teardown_function-------->')def setup(): print('setup---------->