Auto-testing with pytest

You can setup auto-testing by using WatchdogApp and PyTestRunnerApp.

Here is the example jaffle.hcl, which can be used by jaffle start.

 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
kernel "py_kernel" {}

app "watchdog" {
  class  = "jaffle.app.watchdog.WatchdogApp"
  kernel = "py_kernel"

  options {
    handlers = [{
      watch_path         = "pytest_example"
      patterns           = ["*.py"]
      ignore_directories = true
      code_blocks        = ["pytest.handle_watchdog_event({event})"]
    }]
  }
}

app "pytest" {
  class  = "jaffle.app.pytest.PyTestRunnerApp"
  kernel = "py_kernel"

  options {
    args = ["-s", "-v", "--color=yes"]

    auto_test = [
      "pytest_example/tests/test_*.py",
    ]

    auto_test_map {
      "pytest_example/**/*.py" = "pytest_example/tests/{}/test_{}.py"
    }
  }
}
  • L1: Define the kernel py_kernel which is used by watchdog and pytest.
  • L3-5: Create WatchdogApp with name watchdog in the kernel py_kernel.
  • L9-11: Let Watchdog watch the directory pytest_example with provided patterns.
  • L12: When an event comes, the handler executes this code block. The variable pytest is an app created later (L17).
  • L17-19: Define PyTestRunnerApp with name pytest in the kernel py_kernel.
  • L24-26: When pytest_example/tests/test_*.py is modified, pytest executes it.
  • L28-30: When pytest_example/**/*.py is modified, pytest executes the file matched to the pattern pytest_example/tests/{}/test_{}.py.

Interactive Shell

You can also use the interactive shell which attaches the session to the running pytest instance:

$ jaffle attach pytest

When you hit t TAB /, test cases are auto-completed.

Screenshot

../_images/pytest_example.gif

Note

The source package of Jaffle contains example projects in examples directory. You can see the latest version of them here: https://github.com/yatsu/jaffle/tree/master/examples

A pytest example is here: https://github.com/yatsu/jaffle/tree/master/examples/pytest