pytest

py.test can't import my module

冷暖自知 提交于 2019-12-07 04:17:07
问题 I am struggeling getting a python import right. What I want to achieve is to have a module with several source files and a test folder with unit tests. No matter what I do, I can't get py.test-3 to execute my tests. My directory layout looks like this: . ├── module │ ├── __init__.py │ └── testclass.py └── tests └── test_testclass.py The __init__.py file looks like this: __all__ = ['testclass'] The testclass.py file looks like this: class TestClass(object): def __init__(self): self.id = 1 And

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

余生颓废 提交于 2019-12-07 04:11:28
问题 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

django-pytest setup_method database issue

社会主义新天地 提交于 2019-12-07 03:50:31
问题 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

nose2 vs py.test with isolated processes

好久不见. 提交于 2019-12-07 01:08:54
问题 We have been using nosetest for running and collecting our unittests (which are all written as python unittests which we like). Things we like about nose: uses standard python unit tests (we like the structure this imposes). supports reporting coverage and test output in xml (for jenkins). What we are missing is a good way to run tests in isolated processes while maintaining good error repoorting (we are testing C++ libraries through python so segfaults should not be catastrophic). nosepipe

How can I make py.test tests accept interactive input?

大憨熊 提交于 2019-12-07 00:30:37
问题 I'm using py.test for a somewhat unconventional application. Basically, I want to have user interaction in a test via print() and input() (this is Python 3.5). The ultimate goal is to have semi-automated testing for hardware and multi-layered software which cannot be automatically tested even in principle. Some test cases will ask the testing technician to do something (to be confirmed by enter or press any key or similar on the console) or ask them to take a simple measurement or visually

Layout and importing for pytest in python3

亡梦爱人 提交于 2019-12-06 23:48:23
问题 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 '

How to mock/set system date in pytest?

删除回忆录丶 提交于 2019-12-06 23:23:39
问题 In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this? 回答1: AFAIK, you can't mock builtin methods. One approach I have often done is to change my code a bit to not use datetime directly to obtain the date, but a wrapper function somewhere: # mymodule.py def get_today(): return datetime.date.today() This makes it trivial to just mock it in your test: def test_something(): with mock

Pytest plugin: Overriding pytest_runtest_call and friends

﹥>﹥吖頭↗ 提交于 2019-12-06 22:12:46
问题 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

I want to use stdin in a pytest test

左心房为你撑大大i 提交于 2019-12-06 21:52:10
问题 The PyTest documentation states that stdin is redirected to null as no-one will want to do interactive testing in a batch test context. This is true, but interactive is not the only use of stdin. I want to test code that uses stdin just as it would use any other file. I am happy with stdout and sterr being captured but how to actually have stdin connected to an io.StringIO object say in a PyTest conformant way? 回答1: You can mock it: def test_method(monkeypatch): monkeypatch.setattr('sys.stdin

py.test session level fixtures in setup_method

China☆狼群 提交于 2019-12-06 21:27:38
问题 Is there a way to somehow use pytest fixtures from conftest.py in a test class's setup ? I need to initialize an object when the session starts and use it in some test classes' setup. Something like this: # conftest.py: import pytest @pytest.fixture(scope="session", autouse=True) def myfixture(request): return "myfixture" # test_aaa.py class TestAAA(object): def setup(self, method, myfixture): print("setup myfixture: {}".format(myfixture)) ... 回答1: I used this kind of a setup for a test class