pytest

Cannot import settings in Django

烂漫一生 提交于 2019-12-13 02:18:53
问题 I need to test my Django app with py.test. Hence, I need to tell py.test which is Django's settings module. The command I use for py.test is: ~/GitHub/django-training$ py.test --ds=training.settings Unfortunately, I get this error: ERROR: Could not import settings 'training.settings' (Is it on sys.path?): No module named training.settings Is looks very odd, because my folder structure looks like this: django-training training settings.py and my sys.path contains the /home/user/GitHub/django

Unpacking multiple arguments in pytest

拟墨画扇 提交于 2019-12-12 20:19:50
问题 I stumbled upon this question that has no answer. The OP asks to unpack several variables (using pytest fixtures) and make them have their local name available to the testing function. I thought I had a clean solution, involving automatic unpacking of a dictionary: import pytest @pytest.fixture def my_fix(): return {"A" : 4, "B": 6 } def test_something(my_fix): locals().update(my_fix) assert A == 4 assert B == 6 This is inspired by this answer in Quora. When I run this test using pytest, it

Bazel and py_test in sandbox - any way to define outputs?

て烟熏妆下的殇ゞ 提交于 2019-12-12 20:18:11
问题 I'm running multiple py_test() configurations on number of projects. Since there's a plenty of them, the default sandboxing mechanism seems convenient - tests don't intrude one another, and run in parallel for free. This comes with a cost, though, as to my understanding sandboxing will cause bazel to run the tests in temporary directories. Combined with py_test rule not defining any outs parameter (https://docs.bazel.build/versions/master/be/python.html), this likely means no generated file

How to run server as fixture for py.test

a 夏天 提交于 2019-12-12 18:25:48
问题 I want to write Selenium tests with server as fixture: import pytest @pytest.fixture() def driver(request): from selenium import webdriver d = webdriver.Firefox() request.addfinalizer(d.close) return d @pytest.fixture() def server(): from server import run run(host="localhost", port=8080) def test_can_see_echo(driver,server): page = TestPage(driver) page.fill_text_in_input("test") page.click_send() print page.get_returnet_value() Function run in server fixture is bottle run function. The

How can I test if a pytest fixture raises an exception?

a 夏天 提交于 2019-12-12 16:33:22
问题 Use case: In a pytest test suite I have a @fixture which raises exceptions if command line options for its configuration are missing. I've written a test for this fixture using xfail : import pytest from <module> import <exception> @pytest.mark.xfail(raises=<exception>) def test_fixture_with_missing_options_raises_exception(rc_visard): pass However the output after running the tests does not state the test as passed but "xfailed" instead: ============================== 1 xfailed in 0.15

Mocking a RelatedManager in Django 2

做~自己de王妃 提交于 2019-12-12 16:30:29
问题 This question is directly related to this question, but that one is now outdated it seems. I am trying to test a view without having to access the database. To do that I need to Mock a RelatedManager on the user. I am using pytest and pytest-mock . models.py # truncated for brevity, taken from django-rest-knox class AuthToken(models.Model): user = models.ForeignKey( User, null=False, blank=False, related_name='auth_token_set', on_delete=models.CASCADE ) views.py class ChangeEmail(APIView):

Jenkins build inside a docker container with generated reports

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 16:24:58
问题 I am new to Jenkins and Docker and even after some researches, I do not find the way to do these things. I want to : Execute pytest and python-coverage on my project inside a docker container. This should generate test and coverage reports Access the generated reports and read them with some Jenkins'plugin. When I try locally with Docker, it is working. I created a Dockerfile which creates a docker image with the libs needed and the source code inside it, then a script is called when the

pytest and why avoid init file

隐身守侯 提交于 2019-12-12 16:14:16
问题 Once upon a time, the pytest document now at https://docs.pytest.org/en/latest/goodpractices.html used to say: avoid “ __init__.py ” files in your test directories. This way your tests can run easily against an installed version of mypkg , independently from the installed package if it contains the tests or not. I don't understand following about that quote: What does it mean by installed version of mypkg ? How can I relate installing an app if I have a simple flask app that only says "hello

pytest module import ImportError: No module named

最后都变了- 提交于 2019-12-12 15:27:42
问题 This question has been asked DOZENS of times before, but every one I've come across doesn't have any working solutions for me. I'm using Python 2.7 and pytest to run tests. Structure is as follows (from the root of my project/git repo): myapp/ myapp/ __init__.py # def main(): # ... stuff # if __name__ == "__main__": # main() __main__.py # import __init__ as myapp # myapp.main() config.yaml # (app config file) myapplib/ __init__.py # (Empty file) config.py # Loads {projectroot}/config.yaml #

Mock a class with tedious __init__

这一生的挚爱 提交于 2019-12-12 14:57:31
问题 I have a class that actually connects to a service and does authentication and stuff, but all of this is well tested somewhere else in my code, I just want to mock in the following test: Object with tedious __init__ class B(object): def __init__(self, username, password): con = connect_to_service() # auth = con.authenticate(username, password) def upload(self) return "Uploaded" class A(object): def send(): b = B('user', 'pass') b.upload() tests.py # Now, I want here to test A, and since it