Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a method before all tests in all classes?

I'm writing selenium tests, with a set of classes, each class containing several tests. Each class currently opens and then closes Firefox, which has two consequences:

  • super slow, opening firefox takes longer than running the test in a class...
  • crashes, because after firefox has been closed, trying to reopen it really quickly, from selenium, results in an 'Error 54'

I could solve the error 54, probably, by adding a sleep, but it would still be super slow.

So, what I'd like to do is reuse the same Firefox instances across all test classes. Which means I need to run a method before all test classes, and another method after all test classes. So, 'setup_class' and 'teardown_class' are not sufficient.

like image 947
Hugh Perkins Avatar asked Jul 23 '13 04:07

Hugh Perkins


People also ask

Which execute method before each test?

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. Show activity on this post. You can use @BeforeMethod annotatation to execute any method before every test.

Will execute the method once before the start of all tests?

It is possible to run a method only once for the entire test class before any of the tests are executed, and prior to any @Before method(s). “Once only setup” are useful for starting servers, opening communications, etc. It's time-consuming to close and re-open resources for each test.

Is it necessary to write the test class to test every class?

Do I need to write a test class for every class I need to test? No. It is a convention to start with one test class per class under test, but it is not necessary. Test classes only provide a way to organize tests, nothing more.

How do you run all tests in JUnit?

In order to run all of the tests in a directory including tests in nested directories you will need to use something like googlecode. junittool box. Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.


2 Answers

Using session fixture as suggested by hpk42 is great solution for many cases, but fixture will run only after all tests are collected.

Here are two more solutions:

conftest hooks

Write a pytest_configure or pytest_sessionstart hook in your conftest.py file:

# content of conftest.py   def pytest_configure(config):     """     Allows plugins and conftest files to perform initial configuration.     This hook is called for every plugin and initial conftest     file after command line options have been parsed.     """   def pytest_sessionstart(session):     """     Called after the Session object has been created and     before performing collection and entering the run test loop.     """   def pytest_sessionfinish(session, exitstatus):     """     Called after whole test run finished, right before     returning the exit status to the system.     """   def pytest_unconfigure(config):     """     called before test process is exited.     """ 

pytest plugin

Create a pytest plugin with pytest_configure and pytest_unconfigure hooks.
Enable your plugin in conftest.py:

# content of conftest.py  pytest_plugins = [     'plugins.example_plugin', ]   # content of plugins/example_plugin.py def pytest_configure(config):     pass   def pytest_unconfigure(config):     pass 
like image 186
draganHR Avatar answered Sep 17 '22 12:09

draganHR


You might want to use a session-scoped "autouse" fixture:

# content of conftest.py or a tests file (e.g. in your tests or root directory)  @pytest.fixture(scope="session", autouse=True) def do_something(request):     # prepare something ahead of all tests     request.addfinalizer(finalizer_function) 

This will run ahead of all tests. The finalizer will be called after the last test finished.

like image 22
hpk42 Avatar answered Sep 18 '22 12:09

hpk42