Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test SetUp/TearDown for whole test suite

I have a Python package that needs access to X11. I want to use Xvfb so that I do not have to have a real X11 installed on the build machines -- Hudson in this case. So, I would like to start a Xvfb server when py.test starts, use that one server for all the tests, then close it down.

How can I do that?


Note: I could start(stop) an Xvfb server in the SetUp(TearDown) in each test class but that has two problem: First, it is wasteful. Second, it does not work due to either strange Fatal IO error 0 (Success) on X server if I terminate the server correctly or I get hanging Xvfb processes that do not die. This is using xvfbwrapper if anyone is interested.

like image 673
Sardathrion - against SE abuse Avatar asked Jan 18 '13 13:01

Sardathrion - against SE abuse


People also ask

How do you set up a pytest setup and teardown?

Method and function level setup/teardowndef setup_method(self, method): """ setup any state tied to the execution of the given method in a class. setup_method is invoked for every test method of a class. """ def teardown_method(self, method): """ teardown any state that was previously setup with a setup_method call.

Does teardown run after every test?

As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.

What is setup and teardown in testing?

setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.

What is pytest teardown?

One that is executed to mop-up side effects after a test is run is called a teardown function. By giving our setup and teardown functions special names pytest will ensure that they are run before and after our test function regardless of what happens in the test function.


2 Answers

You could use pytest-xvfb instead of messing with this… It would be easier.


It is actually fairly simple. Create a file called conftest.py in your project root which contains this:

import pytest
import os
import subprocess
import tempfile

@pytest.fixture(scope="session", autouse=True)
def start_xvfb_server (request):
    tempdir = tempfile.mkdtemp()
    xvfb_cmd = ['Xvfb',
                ':1022',
                '-screen', '0', '800x600x24',
                '-fbdir', tempdir,
                '-noreset'
    ]
    xvfb_proc = subprocess.Popen(xvfb_cmd,
            stdout=open(os.devnull),
            stderr=open(os.devnull),
            shell=False
    )
    request.addfinalizer(xvfb_proc.kill)

Now, all you have to do is to set up each tests to set the DISPLAY to 1022 which is trivial to do.

like image 110
Sardathrion - against SE abuse Avatar answered Oct 19 '22 05:10

Sardathrion - against SE abuse


Alternatively, you can simply define setUpClass / tearDownClass methods, as described in the unittest module documentation: https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUpClass

Since py.test v2.4, they are fully supported. This official documentation page also documents all xunit-style compatible methods: https://pytest.org/latest/xunit_setup.html

like image 23
Lucas Cimon Avatar answered Oct 19 '22 04:10

Lucas Cimon