@@ -481,9 +481,12 @@ own proxy, and only fallback to LG_PROXY.
481481
482482See also :ref: `overview-proxy-mechanism `.
483483
484- Simple Example
485- ~~~~~~~~~~~~~~
486484
485+ Writing and Running Tests
486+ ~~~~~~~~~~~~~~~~~~~~~~~~~
487+
488+ Getting Started: A Minimal Test
489+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
487490As a minimal example, we have a target connected via a USB serial converter
488491('/dev/ttyUSB0') and booted to the Linux shell.
489492The following environment config file (``shell-example.yaml ``) describes how to
@@ -559,8 +562,8 @@ environment config:
559562
560563 pytest has automatically found the test case and executed it on the target.
561564
562- Custom Fixture Example
563- ~~~~~~~~~~~~~~~~~~~~~~
565+ Reusing Setup Code with Custom Fixtures
566+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
564567When writing many test cases which use the same driver, we can get rid of some
565568common code by wrapping the :any: `CommandProtocol ` in a fixture.
566569As pytest always executes the ``conftest.py `` file in the test suite directory,
@@ -597,8 +600,8 @@ With this fixture, we can simplify the ``test_example.py`` file to:
597600
598601 ... 1 passed...
599602
600- Strategy Fixture Example
601- ~~~~~~~~~~~~~~~~~~~~~~~~
603+ Managing Target States with Strategy Fixtures
604+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
602605When using a :any: `Strategy ` to transition the target between states, it is
603606useful to define a function scope fixture per state in ``conftest.py ``:
604607
@@ -696,116 +699,74 @@ For this example, you should get a report similar to this:
696699
697700 ========================== 3 passed in 29.77 seconds ===========================
698701
699- Feature Flags
700- ~~~~~~~~~~~~~
701- labgrid includes support for feature flags on a global and target scope.
702- Adding a ``@pytest.mark.lg_feature `` decorator to a test ensures it is only
703- executed if the desired feature is available:
702+ .. _usage_pytestplugin_mark_lg_feature :
703+
704+ @pytest.mark.lg_feature()
705+ ~~~~~~~~~~~~~~~~~~~~~~~~~
706+ labgrid supports :ref: `environment-configuration-feature-flags ` in the
707+ :ref: `environment-configuration `.
708+ Adding a ``@pytest.mark.lg_feature() `` decorator to a test ensures it is only
709+ executed if the desired feature is set, either under the target or global
710+ ``features: `` keys.
704711
705712.. code-block :: python
706- :name: test_feature_flags.py
707713
708714 import pytest
709715
710716 @pytest.mark.lg_feature (" camera" )
711717 def test_camera (target ):
712718 pass
713719
714- Here's an example environment configuration:
715-
716- .. code-block :: yaml
717- :name : feature-flag-env.yaml
718-
719- targets :
720- main :
721- features :
722- - camera
723- resources : {}
724- drivers : {}
725-
726- .. testcode :: pytest-example
727- :hide:
728-
729- import pytest
730-
731- plugins = ['labgrid.pytestplugin']
732- pytest.main(['--lg-env', 'feature-flag-env.yaml', 'test_feature_flags.py'], plugins)
720+ In case the feature is unavailable, pytest will record the missing feature
721+ as the skip reason.
733722
734- .. testoutput :: pytest-example
735- :hide:
736-
737- ... 1 passed...
738-
739- This would run the above test, however the following configuration would skip the
740- test because of the missing feature:
741-
742- .. code-block :: yaml
743- :name : feature-flag-skip-env.yaml
744-
745- targets :
746- main :
747- features :
748- - console
749- resources : {}
750- drivers : {}
751-
752- .. testcode :: pytest-example
753- :hide:
754-
755- import pytest
756-
757- plugins = ['labgrid.pytestplugin']
758- pytest.main(['--lg-env', 'feature-flag-skip-env.yaml', 'test_feature_flags.py'], plugins)
759-
760- .. testoutput :: pytest-example
761- :hide:
762-
763- ... 1 skipped...
764-
765- pytest will record the missing feature as the skip reason.
766-
767- For tests with multiple required features, pass them as a list to pytest:
723+ Tests requiring multiple features are also possible:
768724
769725.. code-block :: python
770- :name: test_feature_flags_global.py
771726
772727 import pytest
773728
774729 @pytest.mark.lg_feature ([" camera" , " console" ])
775730 def test_camera (target ):
776731 pass
777732
778- Features do not have to be set per target, they can also be set via the global
779- features key:
780-
781- .. code-block :: yaml
782- :name : feature-flag-global-env.yaml
783-
784- features :
785- - camera
786- targets :
787- main :
788- features :
789- - console
790- resources : {}
791- drivers : {}
733+ @pytest.mark.lg_xfail_feature()
734+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
735+ labgrid supports :ref: `environment-configuration-feature-flags ` in the
736+ :ref: `environment-configuration `.
737+ pytest supports the ``xfail `` marker, see
738+ `pytest.mark.xfail() <https://docs.pytest.org/en/stable/reference/reference.html#pytest-mark-xfail >`_.
792739
793- .. testcode :: pytest-example
794- :hide:
740+ When having more specific features, tests can be marked as `` xfail `` for a
741+ particular feature.
795742
796- import pytest
743+ Imagine two targets have ``camera `` feature flags.
744+ One of them has the additional ``special-camera-2000 `` feature flag.
745+ The other has the additional ``special-camera-3000 `` feature flag.
746+ Due to a known bug on ``special-camera-3000 ``, the test is expected to
747+ fail.
748+ The test can be marked as ``xfail `` for that feature:
797749
798- plugins = ['labgrid.pytestplugin']
799- pytest.main(['--lg-env', 'feature-flag-global-env.yaml', 'test_feature_flags_global.py'],
800- plugins)
750+ .. code-block :: python
801751
802- .. testoutput :: pytest-example
803- :hide:
752+ import pytest
804753
805- ... 1 passed...
754+ @pytest.mark.lg_feature (" camera" ])
755+ @pytest.mark.lg_xfail_feature (
756+ " special-camera-3000" ,
757+ reason = " known bug xy on special-camera-3000" ,
758+ raises = AssertionError ,
759+ strict = True ,
760+ )
761+ def test_camera (target ):
762+ pass
806763
807- This YAML configuration would combine both the global and the target features .
764+ Features under the target and global `` features: `` keys are considered .
808765
766+ ``@pytest.mark.lg_xfail_feature(feature, **kwargs) ``:
767+ - ``feature `` (str) - Feature that should mark the test as ``xfail ``, passed
768+ as boolean ``condition= `` to ``pytest.mark.xfail() ``.
769+ - ``**kwargs `` - All kw-only args are passed to ``pytest.mark.xfail() ``.
809770
810771Test Reports
811772~~~~~~~~~~~~
0 commit comments