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 |
| 20 | |
| 21 | |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 22 | @pytest.fixture(scope='class', autouse=True) |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 23 | def mock_cidb_connection(): |
| 24 | """Ensure that the CIDB connection factory is initialized as a mock. |
| 25 | |
| 26 | Unit tests should never connect to any live instances of CIDB and this |
| 27 | initialization ensures that they only ever get a mock connection instance. |
| 28 | |
| 29 | Previously cros_test_lib.TestProgram.runTests was responsible for globally |
| 30 | initializing this mock and multiple tests are flaky if this mock connection |
| 31 | is not initialized before any tests are run. |
| 32 | """ |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 33 | # pylint: disable=protected-access |
| 34 | cidb.CIDBConnectionFactory._ClearCIDBSetup() |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 35 | cidb.CIDBConnectionFactory.SetupMockCidb() |
Chris McDonald | ffe2a41 | 2020-04-15 02:27:25 -0600 | [diff] [blame] | 36 | |
| 37 | |
| 38 | @pytest.fixture(scope='class', autouse=True) |
| 39 | def assert_no_zombies(): |
| 40 | """Assert that tests have no active child processes after completion. |
| 41 | |
| 42 | This assertion runs after class tearDown methods because of the scope='class' |
| 43 | declaration. |
| 44 | """ |
| 45 | yield |
| 46 | children = multiprocessing.active_children() |
| 47 | if children: |
| 48 | pytest.fail('Test has %s active child processes after tearDown: %s' % |
| 49 | (len(children), children)) |
Chris McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame^] | 50 | |
| 51 | |
| 52 | @pytest.fixture |
| 53 | def legacy_capture_output(request, capfd): |
| 54 | """Adds the `capfd` fixture to TestCase-style test classes. |
| 55 | |
| 56 | This fixture should only be used on cros_test_lib.TestCase test classes, since |
| 57 | it doesn't yield anything and just attaches the built-in pytest `capfd` |
| 58 | fixture to the requesting class. Tests written as standalone functions should |
| 59 | use pytest's built-in `capfd` fixture instead of this. See the documentation |
| 60 | for more information on how to use the `capfd` fixture that this provides: |
| 61 | https://docs.pytest.org/en/latest/reference.html#capfd |
| 62 | |
| 63 | See the following documentation for an explanation of why fixtures have to be |
| 64 | provided to TestCase classes in this manner: |
| 65 | https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks |
| 66 | """ |
| 67 | request.cls.capfd = capfd |