Multihost Hosts
MultihostHost has access to the host part of the
configuration file. Its main purpose is to setup and teardown the host,
preparing it to run tests. It also provides the main connection to the host
making it possible to run remote commands (see
conn attribute).
The host objects are created once when pytest starts and live for the whole duration of the pytest session. Therefore they can be used to store data that should be available to all tests.
Note
The host class can be also used to provide high-level API for your project,
however remember that the test has direct access to the
MultihostRole objects and only indirect access to the host objects
(via the role). Therefore the vast majority of your high-level API should be
placed in the role object in order to provide most direct access.
1class ClientHost(MultihostHost[MyProjectDomain]):
2 def pytest_setup(self) -> None:
3 """
4 Called once before execution of any tests.
5 """
6 # Run your setup code here.
7 # Do not forget to call the parent setup as well.
8 super().pytest_setup()
9 self.conn.run("echo 'Setting up'")
10
11 def pytest_teardown(self) -> None:
12 """
13 Called once after all tests are finished.
14 """
15 # Run your teardown code here.
16 # Do not forget to call the parent teardown as well.
17 self.conn.run("echo 'Tearing down'")
18 super().pytest_teardown()
19
20 def setup(self) -> None:
21 """
22 Called before execution of each test.
23 """
24 # Run your setup code here.
25 # Do not forget to call the parent setup as well.
26 super().setup()
27 self.conn.run("echo 'Setting up'")
28
29 def teardown(self) -> None:
30 """
31 Called after execution of each test.
32 """
33 # Run your teardown code here.
34 # Do not forget to call the parent teardown as well.
35 self.conn.run("echo 'Tearing down'")
36 super().teardown()
See also
There are several methods where you can place your setup and teardown code. See Setup and Teardown Hooks.
See also
The host class is a perfect place to implement host-level backup and
teardown. See Host Backup and Restore for tips on how to
achieve that with MultihostBackupHost.
It is also possible to add custom configuration options or further extend
functionality by overriding the parent class methods. The configuration
dictionary can be accessed by confdict, however
it is recommended to place custom options under the config field which can
by accessed through the config attribute. This
way, it is possible to avoid collisions if pytest-mh introduces new options in
the future.
1class ClientHost(MultihostHost[MyProjectDomain]):
2 @property
3 def required_fields(self) -> list[str]:
4 """
5 Fields that must be set in the host configuration. An error is raised
6 if any field is missing.
7
8 The field name may contain a ``.`` to check nested fields.
9 """
10 return super().required_fields + ["config.my_host_required_option"]
11
12 @property
13 def my_host_option(self) -> bool:
14 return self.config.get("my_host_option", False)
15
16 @property
17 def my_host_required_option(self) -> bool:
18 # This option is required and pytest will error if
19 # it is not present in the configuration
20 return self.config.get("my_host_required_option")
1domains:
2- id: example
3 hosts:
4 - hostname: client.test
5 role: client
6 config:
7 my_host_option: True
8 my_host_required_option: True