Created
May 28, 2023 21:11
-
-
Save rpoisel/32b1edf2bba0de0e43e7cd729aa7fb13 to your computer and use it in GitHub Desktop.
pytest and labgrid introduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export LG_ENV=$(pwd)/inventory.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
targets: | |
main: | |
resources: | |
- NetworkService: | |
address: raspberry-d.lan | |
username: root | |
drivers: | |
- SSHDriver: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[pytest] | |
log_cli = true | |
log_cli_level = INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
def test_example(): | |
logging.info("My first testcase") | |
assert True, "must always pass" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from typing import Iterator | |
import logging | |
@pytest.fixture(scope="session") | |
def myfixt() -> Iterator[int]: | |
logging.info("before") | |
yield 42 | |
logging.info("after") | |
def test_fixture(myfixt): | |
assert myfixt == 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from labgrid.target import Target | |
from labgrid.driver import SSHDriver | |
from labgrid.driver.exception import ExecutionError | |
import pytest | |
from typing import Iterator | |
import logging | |
@pytest.fixture(scope='session') | |
def shell_cmd(target: Target) -> Iterator[SSHDriver]: | |
cmd = target.get_driver('SSHDriver') | |
target.activate(cmd) | |
yield cmd | |
def test_uname_system(shell_cmd: SSHDriver): | |
result = '\n'.join(shell_cmd.run_check("uname -s")) | |
logging.info(result) | |
assert 'Linux' == result | |
def test_command_fails_system(shell_cmd: SSHDriver): | |
with pytest.raises(ExecutionError, match='command not found'): | |
shell_cmd.run_check("program does not exist and fails therefore") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment