File: test_steplogger.py

package info (click to toggle)
labgrid 25.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,796 kB
  • sloc: python: 21,352; sh: 846; makefile: 35
file content (58 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pexpect
import pytest
import time
import re

@pytest.fixture
def power_env(tmpdir):
    p = tmpdir.join("config.yaml")
    p.write(
"""
targets:
  main:
    drivers:
      ManualPowerDriver: {}
"""
    )
    return p

@pytest.fixture
def pw_cycle_test(tmpdir):
    t = tmpdir.join("pw_cycle_test.py")
    t.write(
"""
def test(target):
    pw = target.get_driver("ManualPowerDriver")
    pw.cycle()
"""
    )
    return t

def test_step_logger(power_env, pw_cycle_test):
    from colors import strip_color
    # step reporter is called when -vv is given
    # -s is necessary for manual power driver confirmation
    with pexpect.spawn('pytest -vvs --lg-env {env} {test}'
                       .format(env=power_env, test=pw_cycle_test)) as spawn:

        # rough match
        spawn.expect("→.*?ManualPowerDriver.*?cycle.*?\r\n".encode("utf-8"))
        step_line = strip_color(spawn.after.decode("utf-8")).rstrip()
        # exact match
        assert step_line.endswith("→ ManualPowerDriver.cycle()"), f"'{step_line}' did not match"

        spawn.expect("main: CYCLE the target main and press enter")
        time.sleep(0.02) # ensure that the step measures a duration
        spawn.sendline()

        # rough match
        spawn.expect("←.*?ManualPowerDriver.*?cycle.*?\r\n".encode("utf-8"))
        step_line = strip_color(spawn.after.decode("utf-8")).rstrip()
        # exact match

        assert re.match(r"← ManualPowerDriver.cycle\(\) \[[\d.]+s\]$", step_line), f"'{step_line}' did not match"

        spawn.expect(pexpect.EOF)
        spawn.close()
        assert spawn.exitstatus == 0