|
| 1 | +############## |
| 2 | + Key Concepts |
| 3 | +############## |
| 4 | + |
| 5 | +********** |
| 6 | + Fixtures |
| 7 | +********** |
| 8 | + |
| 9 | +Each test case initializes a few fixtures. The most important fixtures are: |
| 10 | + |
| 11 | +- ``msg_queue``: A message queue. A background listener process reads all messages from this queue, logs them to the terminal with an optional timestamp, and records them to the pexpect process. |
| 12 | +- ``pexpect_proc``: A pexpect process that can run ``pexpect.expect()`` for testing purposes. |
| 13 | +- ``app``: The built binary. |
| 14 | +- ``dut``: Device Under Test (DUT). A DUT contains several daemon processes/threads, and the output of each is redirected to the ``msg_queue`` fixture. |
| 15 | + |
| 16 | +.. note:: |
| 17 | + |
| 18 | + You may redirect any output to the ``msg_queue`` fixture by ``contextlib.redirect_stdout``. |
| 19 | + |
| 20 | + .. code:: python |
| 21 | +
|
| 22 | + import contextlib |
| 23 | +
|
| 24 | + def test_redirect(dut, msg_queue): |
| 25 | + with contextlib.redirect_stdout(msg_queue): |
| 26 | + print("will be redirected") |
| 27 | +
|
| 28 | + dut.expect_exact("redirected") |
| 29 | +
|
| 30 | + Or you may redirect the output from a fixture ``redirect`` |
| 31 | + |
| 32 | + .. code:: python |
| 33 | +
|
| 34 | + def test_redirect(dut, msg_queue, redirect): |
| 35 | + with redirect(): |
| 36 | + print("will also be redirected") |
| 37 | +
|
| 38 | + dut.expect_exact("redirected") |
| 39 | +
|
| 40 | +You can run ``pytest --fixtures`` to see all fixtures defined by ``pytest-embedded``. They are listed under the ``fixtures defined from pytest_embedded.plugin`` section. |
| 41 | + |
| 42 | +***************** |
| 43 | + Parametrization |
| 44 | +***************** |
| 45 | + |
| 46 | +All CLI options support parametrization via ``indirect=True``. Parametrization is a feature provided by ``pytest``. For more details, please refer to the `Parametrizing tests <https://docs.pytest.org/en/stable/example/parametrize.html>`_ documentation. |
| 47 | + |
| 48 | +For example, running the ``pytest`` command with the following test script: |
| 49 | + |
| 50 | +.. code:: python |
| 51 | +
|
| 52 | + @pytest.mark.parametrize( |
| 53 | + "embedded_services, app_path", |
| 54 | + [ |
| 55 | + ("idf", app_path_1), |
| 56 | + ("idf", app_path_2), |
| 57 | + ], |
| 58 | + indirect=True, |
| 59 | + ) |
| 60 | + def test_serial_tcp(dut): |
| 61 | + assert dut.app.target == "esp32" |
| 62 | + dut.expect("Restart now") |
| 63 | +
|
| 64 | +is equivalent to running two separate commands, ``pytest --embedded-services idf --app-path <app_path_1>`` and ``pytest --embedded-services idf --app-path <app_path_2>``, with this test script: |
| 65 | + |
| 66 | +.. code:: python |
| 67 | +
|
| 68 | + def test_serial_tcp(dut): |
| 69 | + assert dut.app.target == "esp32" |
| 70 | + dut.expect("Restart now") |
| 71 | +
|
| 72 | +********** |
| 73 | + Services |
| 74 | +********** |
| 75 | + |
| 76 | +You can activate additional services with ``pytest --embedded-services service[,service]`` to enable extra fixtures and functionalities. These services are provided by optional dependencies, which can be installed with ``pip``. |
| 77 | + |
| 78 | +Available services are described :doc:`here <services>`. |
| 79 | + |
| 80 | +************ |
| 81 | + Multi DUTs |
| 82 | +************ |
| 83 | + |
| 84 | +Sometimes, you may need multiple DUTs for testing, for example, in master-slave or mesh network tests. |
| 85 | + |
| 86 | +Here are a few examples of how to enable this feature. For detailed information, refer to the ``embedded`` group in the ``pytest --help`` output. |
| 87 | + |
| 88 | +Enable multi DUTs by specifying ``--count`` |
| 89 | +=========================================== |
| 90 | + |
| 91 | +When multi-DUT mode is enabled, all fixtures become a tuple of instances. Each instance in the tuple is independent. For parametrization, each configuration uses ``|`` as a separator for each instance's values. |
| 92 | + |
| 93 | +For example, running shell command: |
| 94 | + |
| 95 | +.. code:: shell |
| 96 | +
|
| 97 | + pytest \ |
| 98 | + --embedded-services serial|serial \ |
| 99 | + --count 2 \ |
| 100 | + --app-path <master_bin>|<slave_bin> |
| 101 | +
|
| 102 | +enables two DUTs with the ``serial`` service. The ``app`` fixture becomes a tuple of two ``App`` instances, and ``dut`` becomes a tuple of two ``Dut`` instances. |
| 103 | + |
| 104 | +You can test with: |
| 105 | + |
| 106 | +.. code:: python |
| 107 | +
|
| 108 | + def test(dut): |
| 109 | + master = dut[0] |
| 110 | + slave = dut[1] |
| 111 | +
|
| 112 | + master.expect("sent") |
| 113 | + slave.expect("received") |
| 114 | +
|
| 115 | +Specify once when applying to all DUTs |
| 116 | +====================================== |
| 117 | + |
| 118 | +If all DUTs share the same configuration value, you only need to specify it once. |
| 119 | + |
| 120 | +.. code:: shell |
| 121 | +
|
| 122 | + pytest \ |
| 123 | + --embedded-services serial \ |
| 124 | + --count 2 \ |
| 125 | + --app-path <master_bin>|<slave_bin> |
| 126 | +
|
| 127 | +The ``--embedded-services serial`` option applies to all DUTs. |
| 128 | + |
| 129 | +Vacant Value if it's Useless |
| 130 | +============================ |
| 131 | + |
| 132 | +Sometimes, an option is only useful for a specific service. You can provide a vacant value if a configuration is not applicable to a particular DUT. |
| 133 | + |
| 134 | +.. code:: shell |
| 135 | +
|
| 136 | + pytest \ |
| 137 | + --embedded-services qemu|serial \ |
| 138 | + --count 2 \ |
| 139 | + --app-path <master_bin>|<slave_bin> \ |
| 140 | + --qemu-cli-args "<args>|" \ |
| 141 | + --port "|<port>" |
| 142 | +
|
| 143 | +The ``--qemu-cli-args`` option applies to the first DUT (with the ``qemu`` service), while ``--port`` applies to the second DUT (with the ``serial`` service). |
| 144 | + |
| 145 | +********* |
| 146 | + Logging |
| 147 | +********* |
| 148 | + |
| 149 | +``pytest-embedded`` prints all DUT output with a timestamp. To remove the timestamp, run pytest with the ``--with-timestamp n`` option. |
| 150 | + |
| 151 | +By default, ``pytest`` swallows ``stdout``. To see the live output, run pytest with the ``-s`` option. |
0 commit comments