blob: 023cc2156d80c70b499edc9164086af62a9f7749 [file] [log] [blame]
Chris McDonald2e9a09c2020-04-03 16:09:32 -06001# -*- coding: utf-8 -*-
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Configuration and fixtures for pytest.
7
8See the following doc link for an explanation of conftest.py and how it is used
9by pytest:
10https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions
11"""
12
13from __future__ import print_function
14
Chris McDonaldffe2a412020-04-15 02:27:25 -060015import multiprocessing
16
Chris McDonalde53dde12020-04-07 13:43:14 -060017import pytest # pylint: disable=import-error
Chris McDonald2e9a09c2020-04-03 16:09:32 -060018
19from chromite.lib import cidb
Chris McDonald320ef422020-04-18 04:57:04 -060020from chromite.lib import parallel
Chris McDonald6ce00962020-04-14 04:05:21 -060021from chromite.lib import retry_stats
Chris McDonald2e9a09c2020-04-03 16:09:32 -060022
23
Chris McDonalde53dde12020-04-07 13:43:14 -060024@pytest.fixture(scope='class', autouse=True)
Chris McDonald2e9a09c2020-04-03 16:09:32 -060025def mock_cidb_connection():
26 """Ensure that the CIDB connection factory is initialized as a mock.
27
28 Unit tests should never connect to any live instances of CIDB and this
29 initialization ensures that they only ever get a mock connection instance.
30
31 Previously cros_test_lib.TestProgram.runTests was responsible for globally
32 initializing this mock and multiple tests are flaky if this mock connection
33 is not initialized before any tests are run.
34 """
Chris McDonalde53dde12020-04-07 13:43:14 -060035 # pylint: disable=protected-access
36 cidb.CIDBConnectionFactory._ClearCIDBSetup()
Chris McDonald2e9a09c2020-04-03 16:09:32 -060037 cidb.CIDBConnectionFactory.SetupMockCidb()
Chris McDonaldffe2a412020-04-15 02:27:25 -060038
39
40@pytest.fixture(scope='class', autouse=True)
41def assert_no_zombies():
42 """Assert that tests have no active child processes after completion.
43
44 This assertion runs after class tearDown methods because of the scope='class'
45 declaration.
46 """
47 yield
48 children = multiprocessing.active_children()
49 if children:
50 pytest.fail('Test has %s active child processes after tearDown: %s' %
51 (len(children), children))
Chris McDonalde13f0242020-04-07 18:37:15 -060052
53
Chris McDonald6ce00962020-04-14 04:05:21 -060054@pytest.fixture(scope='class', autouse=True)
55def clear_retry_stats_manager():
56 """Reset the global state of the stats manager before every test.
57
58 Without this fixture many tests fail due to this global value being set and
59 then not cleared. The child manager process may have been killed but this
60 module level variable is still pointing at it, leading to the test trying to
61 write to a closed pipe.
62 """
63 # pylint: disable=protected-access
64 retry_stats._STATS_COLLECTION = None
65
66
Chris McDonalde13f0242020-04-07 18:37:15 -060067@pytest.fixture
Chris McDonald320ef422020-04-18 04:57:04 -060068def singleton_manager(monkeypatch):
69 """Force tests to use a singleton Manager and automatically clean it up."""
70 m = parallel.Manager()
71
72 def our_manager():
73 return m
74
75 monkeypatch.setattr(parallel, 'Manager', our_manager)
76 yield
77 m.shutdown()
78
79
80@pytest.fixture
Chris McDonalde13f0242020-04-07 18:37:15 -060081def legacy_capture_output(request, capfd):
82 """Adds the `capfd` fixture to TestCase-style test classes.
83
84 This fixture should only be used on cros_test_lib.TestCase test classes, since
85 it doesn't yield anything and just attaches the built-in pytest `capfd`
86 fixture to the requesting class. Tests written as standalone functions should
87 use pytest's built-in `capfd` fixture instead of this. See the documentation
88 for more information on how to use the `capfd` fixture that this provides:
89 https://docs.pytest.org/en/latest/reference.html#capfd
90
91 See the following documentation for an explanation of why fixtures have to be
92 provided to TestCase classes in this manner:
93 https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks
94 """
95 request.cls.capfd = capfd
Chris McDonald6ce00962020-04-14 04:05:21 -060096
97
98@pytest.fixture
99def testcase_caplog(request, caplog):
100 """Adds the `caplog` fixture to TestCase-style test classes.
101
102 This fixture should only be used on cros_test_lib.TestCase test classes, since
103 it doesn't yield anything and just attaches the built-in pytest `caplog`
104 fixture to the requesting class. Tests written as standalone functions should
105 use pytest's built-in `caplog` fixture instead of this. See the documentation
106 for more information on how to use the `caplog` fixture that this provides:
107 https://docs.pytest.org/en/latest/reference.html#caplog
108
109 See the following documentation for an explanation of why fixtures have to be
110 provided to TestCase classes in this manner:
111 https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks
112 """
113 request.cls.caplog = caplog