Skip to content

Commit

Permalink
Run test suite with pytest
Browse files Browse the repository at this point in the history
pytest has better support for parameterized tests. Each test iteration
appears as a separate dot by the test runner and can run in isolation.

Reviewed-by: François Freitag
  • Loading branch information
jdufresne committed Mar 26, 2019
1 parent aaf3bd9 commit 4c9d4ca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
46 changes: 46 additions & 0 deletions tests/test_html2docx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import json
import pathlib

import docx
import pytest

from html2docx import html2docx


def generate_testdata():
datadir = pathlib.Path(__file__).parent.joinpath("data")
for html_path in datadir.glob("*.html"):
spec_path = datadir.joinpath(f"{html_path.stem}.json")
yield html_path, spec_path


@pytest.mark.parametrize("html_path,spec_path", generate_testdata())
def test_html2docx(html_path, spec_path):
title = html_path.name
with html_path.open() as fp:
html = fp.read()
with spec_path.open() as fp:
spec = json.load(fp)

buf = html2docx(html, title=title)
doc = docx.Document(buf)

assert doc.core_properties.title == title
assert len(doc.paragraphs) == len(spec)
for p, p_spec in zip(doc.paragraphs, spec):
assert p.text == p_spec["text"]
assert p.style.name == p_spec.get("style", "Normal")

runs_spec = p_spec["runs"]
assert len(p.runs) == len(runs_spec)
for run, run_spec in zip(p.runs, runs_spec):
assert run.text == run_spec["text"]
assert run.bold is run_spec.get(
"bold", False
), f"Wrong bold for text '{run.text}'."
assert run.italic is run_spec.get(
"italic", False
), f"Wrong italic for text '{run.text}'."
assert run.underline is run_spec.get(
"underline", False
), f"Wrong underline for text '{run.text}'."
48 changes: 0 additions & 48 deletions tests/tests.py

This file was deleted.

3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ envlist =
minversion = 1.9

[testenv]
commands = python -m unittest
commands = pytest {posargs}
deps = pytest

[testenv:black]
commands = black --target-version=py36 --check --diff .
Expand Down

0 comments on commit 4c9d4ca

Please sign in to comment.