@@ -500,6 +500,7 @@ The following environment config file (``shell-example.yaml``) describes how to
500500access this board:
501501
502502.. code-block :: yaml
503+ :name : shell-example.yaml
503504
504505 targets :
505506 main :
@@ -516,6 +517,7 @@ access this board:
516517 We then add the following test in a file called ``test_example.py ``:
517518
518519.. code-block :: python
520+ :name: test_shell.py
519521
520522 def test_echo (target ):
521523 command = target.get_driver(' CommandProtocol' )
@@ -525,6 +527,35 @@ We then add the following test in a file called ``test_example.py``:
525527 To run this test, we simply execute pytest in the same directory with the
526528environment config:
527529
530+ .. testsetup :: pytest-example
531+
532+ from labgrid.driver import SerialDriver, ShellDriver
533+
534+ patch('serial.Serial').start()
535+ patch.object(
536+ SerialDriver,
537+ '_read',
538+ Mock(return_value=b'root@example:~ ')
539+ ).start()
540+ patch.object(
541+ ShellDriver,
542+ '_run',
543+ Mock(return_value=(['OK'], [], 0))
544+ ).start()
545+
546+ .. testcode :: pytest-example
547+ :hide:
548+
549+ import pytest
550+
551+ plugins = ['labgrid.pytestplugin']
552+ pytest.main(['--lg-env', 'shell-example.yaml', 'test_shell.py'], plugins)
553+
554+ .. testoutput :: pytest-example
555+ :hide:
556+
557+ ... 1 passed...
558+
528559.. code-block :: bash
529560
530561 $ pytest --lg-env shell-example.yaml --verbose
@@ -546,6 +577,7 @@ As pytest always executes the ``conftest.py`` file in the test suite directory,
546577we can define additional fixtures there:
547578
548579.. code-block :: python
580+ :name: conftest_fixture.py
549581
550582 import pytest
551583
@@ -556,11 +588,25 @@ we can define additional fixtures there:
556588 With this fixture, we can simplify the ``test_example.py `` file to:
557589
558590.. code-block :: python
591+ :name: test_custom_fixture.py
559592
560593 def test_echo (command ):
561594 result = command.run_check(' echo OK' )
562595 assert ' OK' in result
563596
597+ .. testcode :: pytest-example
598+ :hide:
599+
600+ import pytest
601+
602+ plugins = ['labgrid.pytestplugin', 'conftest_fixture']
603+ pytest.main(['--lg-env', 'shell-example.yaml', 'test_custom_fixture.py'], plugins)
604+
605+ .. testoutput :: pytest-example
606+ :hide:
607+
608+ ... 1 passed...
609+
564610Strategy Fixture Example
565611~~~~~~~~~~~~~~~~~~~~~~~~
566612When using a :any: `Strategy ` to transition the target between states, it is
@@ -667,6 +713,7 @@ Adding a ``@pytest.mark.lg_feature`` decorator to a test ensures it is only
667713executed if the desired feature is available:
668714
669715.. code-block :: python
716+ :name: test_feature_flags.py
670717
671718 import pytest
672719
@@ -677,6 +724,7 @@ executed if the desired feature is available:
677724 Here's an example environment configuration:
678725
679726.. code-block :: yaml
727+ :name : feature-flag-env.yaml
680728
681729 targets :
682730 main :
@@ -685,10 +733,24 @@ Here's an example environment configuration:
685733 resources : {}
686734 drivers : {}
687735
736+ .. testcode :: pytest-example
737+ :hide:
738+
739+ import pytest
740+
741+ plugins = ['labgrid.pytestplugin']
742+ pytest.main(['--lg-env', 'feature-flag-env.yaml', 'test_feature_flags.py'], plugins)
743+
744+ .. testoutput :: pytest-example
745+ :hide:
746+
747+ ... 1 passed...
748+
688749This would run the above test, however the following configuration would skip the
689750test because of the missing feature:
690751
691752.. code-block :: yaml
753+ :name : feature-flag-skip-env.yaml
692754
693755 targets :
694756 main :
@@ -697,11 +759,25 @@ test because of the missing feature:
697759 resources : {}
698760 drivers : {}
699761
762+ .. testcode :: pytest-example
763+ :hide:
764+
765+ import pytest
766+
767+ plugins = ['labgrid.pytestplugin']
768+ pytest.main(['--lg-env', 'feature-flag-skip-env.yaml', 'test_feature_flags.py'], plugins)
769+
770+ .. testoutput :: pytest-example
771+ :hide:
772+
773+ ... 1 skipped...
774+
700775pytest will record the missing feature as the skip reason.
701776
702777For tests with multiple required features, pass them as a list to pytest:
703778
704779.. code-block :: python
780+ :name: test_feature_flags_global.py
705781
706782 import pytest
707783
@@ -713,6 +789,7 @@ Features do not have to be set per target, they can also be set via the global
713789features key:
714790
715791.. code-block :: yaml
792+ :name : feature-flag-global-env.yaml
716793
717794 features :
718795 - camera
@@ -723,6 +800,20 @@ features key:
723800 resources : {}
724801 drivers : {}
725802
803+ .. testcode :: pytest-example
804+ :hide:
805+
806+ import pytest
807+
808+ plugins = ['labgrid.pytestplugin']
809+ pytest.main(['--lg-env', 'feature-flag-global-env.yaml', 'test_feature_flags_global.py'],
810+ plugins)
811+
812+ .. testoutput :: pytest-example
813+ :hide:
814+
815+ ... 1 passed...
816+
726817This YAML configuration would combine both the global and the target features.
727818
728819
0 commit comments