Setup and teardown directive example

Setup and teardown directives. Directives are HTML comments and are not rendered. Look for the <!--phmutest-setup--> and <!--phmutest-teardown--> directives in the Markdown file.

The next 2 blocks are marked as the setup blocks

The main reason to use setup blocks is so that the teardown blocks will run in the event a post setup block fails. Note that teardown will only run if all the setup blocks succeed. We use ExitStack to create a single function call to do cleanup:

  • change back to the original working directory
  • cleanup the temporary directory

We use with ExitStack to assure cleanup when a statement in the with suite raises an exception.

import os
import tempfile
from contextlib import ExitStack
from pathlib import Path

FILENAME = "floaters.txt"
CONTENTS = "apples, cider, cherries, very small rocks."
workdir = Path.cwd()

Each setup block must have the <!--phmutest-setup--> directive.

# note callbacks are done in reverse order
with ExitStack() as stack:
    td = stack.enter_context(tempfile.TemporaryDirectory())
    stack.callback(os.chdir, workdir)  # restore
    os.chdir(td)
    cleanup = stack.pop_all().close

Path(FILENAME).write_text(CONTENTS, encoding="utf-8")

This block shows the temporary file exists

print(Path(FILENAME).read_text(encoding="utf-8"))
assert Path.cwd() != workdir, "In a different cwd, presumably tempdir."

expected output:

apples, cider, cherries, very small rocks.

The next 2 blocks are marked as the teardown blocks

Setup and teardown blocks can have an output block as well.

print("Restoring current working directory...")
cleanup()

expected output:

Restoring current working directory...

This block will be designated as teardown code too

More than one setup or teardown block is allowed. Each block must have the <!--phmutest-teardown--> directive. The block shows that cleanup() restored the initial cwd.

assert Path.cwd() == workdir, "The original cwd."

phmutest command line

Note that this fenced code block has txt as the info string. The txt tells phmutest that this is not an output block for the python block immediately above.

phmutest docs/setup/setup.md --log

phmutest output

Terminal output after the OK line.

log:
args.files: 'docs/setup/setup.md'
args.log: 'True'

location|label                     result
---------------------------------  ------
docs/setup/setup.md:24 setup.....  pass
docs/setup/setup.md:39 setup.....  pass
docs/setup/setup.md:52 o.........  pass
docs/setup/setup.md:69 teardown o  pass
docs/setup/setup.md:88 teardown..  pass
---------------------------------  ------