Skip to content

Instantly share code, notes, and snippets.

@rpoisel
Created May 28, 2023 21:11
Show Gist options
  • Save rpoisel/32b1edf2bba0de0e43e7cd729aa7fb13 to your computer and use it in GitHub Desktop.
Save rpoisel/32b1edf2bba0de0e43e7cd729aa7fb13 to your computer and use it in GitHub Desktop.
pytest and labgrid introduction
export LG_ENV=$(pwd)/inventory.yaml
---
targets:
main:
resources:
- NetworkService:
address: raspberry-d.lan
username: root
drivers:
- SSHDriver: {}
[pytest]
log_cli = true
log_cli_level = INFO
import logging
def test_example():
logging.info("My first testcase")
assert True, "must always pass"
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
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