Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | See the following doc link for an explanation of conftest.py and how it is used |
| 9 | by pytest: |
| 10 | https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions |
| 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | |
Chris McDonald | ffe2a41 | 2020-04-15 02:27:25 -0600 | [diff] [blame] | 15 | import multiprocessing |
| 16 | |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 17 | import pytest # pylint: disable=import-error |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 18 | |
| 19 | from chromite.lib import cidb |
Chris McDonald | 320ef42 | 2020-04-18 04:57:04 -0600 | [diff] [blame] | 20 | from chromite.lib import parallel |
Chris McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 21 | from chromite.lib import retry_stats |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 22 | |
| 23 | |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 24 | @pytest.fixture(scope='class', autouse=True) |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 25 | def 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 McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 35 | # pylint: disable=protected-access |
| 36 | cidb.CIDBConnectionFactory._ClearCIDBSetup() |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 37 | cidb.CIDBConnectionFactory.SetupMockCidb() |
Chris McDonald | ffe2a41 | 2020-04-15 02:27:25 -0600 | [diff] [blame] | 38 | |
| 39 | |
| 40 | @pytest.fixture(scope='class', autouse=True) |
| 41 | def 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 McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 52 | |
| 53 | |
Chris McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 54 | @pytest.fixture(scope='class', autouse=True) |
| 55 | def 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 McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 67 | @pytest.fixture |
Chris McDonald | 320ef42 | 2020-04-18 04:57:04 -0600 | [diff] [blame] | 68 | def 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 McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 81 | def 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 McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 96 | |
| 97 | |
| 98 | @pytest.fixture |
| 99 | def 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 |