Hi all. I realise this might be a bit niche/obscure, but hoping someone can give me some pointers.
I'm looking into running tests using pytest from RMarkdown documents. The wider goal is to use RMarkdown documents as a form of executable specifications. The idea is to interleave narrative describing/defining requirements with executable tests that verify the implementation.
Something like this in the context of calculating tax:
An individual earning up to the Personal Tax Allowance pays no tax
import pytest from tax import calculate_net_income def test_personal_allowance(): """There's no tax to pay for income in the Personal Allowance band""" gross = 12500 assert calculate_net_income(gross) == gross pytest.main(args=['-v'])
Whilst this runs,
pytest doesn't find any tests to run:
## ============================= test session starts ============================= ## platform win32 -- Python 3.8.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- C:\sf\bin\R\R-40~1.3\bin\x64\Rterm.exe ## cachedir: .pytest_cache ## rootdir: C:\Users\sfinnie\projects\executable_specifications\RMarkdown ## plugins: jupyter-pytest-2-1.0.1 ## collecting ... collected 0 items ## ## ============================ no tests ran in 0.01s ============================ ## <ExitCode.NO_TESTS_COLLECTED: 5>
I'm assuming that's because there's no actual test module for
pytest to discover (e.g.
test_tax.py). I imagine it might be possible to add a custom step into the
knitr pipeline, for example to extract all python snippets and put them into a file that pytest can find and run. But I've never looked into the pipeline, so no idea if that's viable / if there's a better way.
Any pointers appreciated.