Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run server as fixture for py.test

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 problem is that, when I call run() programs go into infinite loop and body of test is not executed. Should I call run in same thread? Is my design fine? In future I want use server fixture to integrate to server state. For example make test "add comment" using Selenium and in the end use server fixture to ask server if this action really happened.

like image 619
Bartłomiej Bartnicki Avatar asked Feb 20 '16 18:02

Bartłomiej Bartnicki


People also ask

Can a pytest fixture be a test?

Those are the things that need to test a certain action. In pytest the fixtures are functions that we define to serve these purpose, we can pass these fixtures to our test functions (test cases) so that they can run and set up the desired state for you to perform the test.

Where do I put pytest fixtures?

They don't require importing, but if you defined them in a test_*. py file, they'll only be available in that file. You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files. pytest-internal fixtures are simply defined in a core plugin, and thus available everywhere.

What is pytest fixture ()?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.

What is the correct syntax to apply a pytest fixture?

login(user) @pytest. fixture def landing_page(driver, login): return LandingPage(driver) def test_name_on_landing_page_after_login(landing_page, user): assert landing_page. header == f"Welcome, {user.name}!"


1 Answers

The tests hang because your run(host="localhost", port=8080) starts a server which waits forever. You should start that server in a different thread/process.

Look into something like pytest-xprocess for running external server processes for your tests.

like image 163
Oin Avatar answered Oct 08 '22 22:10

Oin