Testing
PyPDFForm uses pytest for testing and coverage.py for measuring test coverage. To run the tests, execute:
coverage run -m pytest && coverage report --fail-under=100
Alternatively, in the development container, run:
test
Generate coverage report
To generate a test coverage report, run:
coverage run -m pytest && coverage html
View the coverage report by opening htmlcov/index.html
in a browser.
Alternatively, in the development container, run:
coverage
And the coverage report can be found at http://localhost:8000/htmlcov/index.html
.
Test breakdown
Although each PyPDFForm test is unique, most follow a general paradigm.
Most tests can be summarized into three steps:
- Define an expected PDF file that the outcome of the test should look like.
- Execute a sequence of code using PyPDFForm to generate a PDF that should look like the expected PDF file.
- Compare the PDF generated by the test with the expected PDF file.
Consider this example test:
def test_fill(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(
os.path.join(pdf_samples, "sample_template.pdf")
).fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()
expected = f.read()
assert len(obj.read()) == len(expected)
assert obj.read() == expected
The test starts by defining an expected PDF sample_filled.pdf
:
expected_path = os.path.join(pdf_samples, "sample_filled.pdf")
The test then fills sample_template.pdf
with a data dictionary using PdfWrapper
:
obj = PdfWrapper(
os.path.join(pdf_samples, "sample_template.pdf")
).fill(
{
"test": "test_1",
"check": True,
"test_2": "test_2",
"check_2": False,
"test_3": "test_3",
"check_3": True,
},
)
Include these two lines in most tests to simplify updating old tests:
request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()
Finally, the test compares the resulted stream from the test with the expected file stream:
expected = f.read()
assert len(obj.read()) == len(expected)
assert obj.read() == expected