Skip to content

Commit

Permalink
Add first draft of pytest automation (see #10)
Browse files Browse the repository at this point in the history
Usage: python -m pytest __tests__
  • Loading branch information
ines committed Apr 21, 2019
1 parent d7af0d2 commit e148eb1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,4 +1,6 @@
__tests__
.vscode
__pycache__

# Logs
logs
Expand Down
2 changes: 2 additions & 0 deletions binder/requirements.txt
@@ -1,5 +1,7 @@
spacy==2.1.3
wasabi>=0.2.1,<1.1.0
srsly>=0.0.5,<1.1.0
pytest>= 4.4.1,<4.5.0

https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz#egg=en_core_web_sm==2.1.0
https://github.com/explosion/spacy-models/releases/download/en_core_web_md-2.1.0/en_core_web_md-2.1.0.tar.gz#egg=en_core_web_md==2.1.0
57 changes: 57 additions & 0 deletions conftest.py
@@ -0,0 +1,57 @@
import pytest
import shutil
from pathlib import Path
from wasabi import Printer
import srsly

TESTS_DIR = "__tests__"
EXERCISES_DIR = "exercises"
META_FILE = "meta.json"
PYTEST_TEMPLATE = "pytestTemplate"

msg = Printer()


def format_test(name, template, test, solution):
full_code = template.replace("${solution}", solution).replace("${test}", test)
# Need to indent the lines to fit it into test function – can probably be less hacky
indented = "\n".join([" " + line for line in full_code.split("\n")])
return "def test_{}():\n{}".format(name, indented)


def get_source_files():
exercises_path = Path(EXERCISES_DIR)
if not exercises_path.exists():
msg.fail("Can't find exercises directory: {}".format(EXERCISES_DIR), exits=1)
for py_file in exercises_path.iterdir():
if py_file.name.startswith("test_"):
solution_name = "solution_{}".format(py_file.name.split("test_")[1])
solution_file = exercises_path / solution_name
if not solution_file.exists():
msg.warn("Didn't find solution for test: {}".format(py_file.stem))
else:
yield (py_file, solution_file)


def pytest_sessionstart(session):
test_dir = Path(TESTS_DIR)
if test_dir.exists():
shutil.rmtree(str(test_dir))
msg.info("Deleted existing test directory {}".format(TESTS_DIR))
test_dir.mkdir()
msg.good("Created test directory {}".format(TESTS_DIR))
meta = srsly.read_json(META_FILE)
n_files = 0
for test_file, solution_file in get_source_files():
with test_file.open("r", encoding="utf8") as f:
test_code = f.read()
with solution_file.open("r", encoding="utf8") as f:
solution_code = f.read()
full_code = format_test(
test_file.stem, meta[PYTEST_TEMPLATE], test_code, solution_code
)
test_path = test_dir / test_file.name
with test_path.open("w", encoding="utf8") as f:
f.write(full_code)
n_files += 1
msg.good("Created {} files for pytest in {}".format(n_files, TESTS_DIR))
1 change: 1 addition & 0 deletions meta.json
Expand Up @@ -7,6 +7,7 @@
"twitter": "spacy_io",
"fonts": "IBM+Plex+Mono:500|IBM+Plex+Sans:700|Lato:400,400i,700,700i",
"testTemplate": "from wasabi import Printer\n__msg__ = Printer()\n__solution__ = \"\"\"${solution}\"\"\"\n\n${solution}\n\n${test}\n\ntry:\n test()\nexcept AssertionError as e:\n __msg__.fail(e)",
"pytestTemplate": "from wasabi import Printer\n__msg__ = Printer()\n__solution__ = \"\"\"${solution}\"\"\"\n\n${solution}\n\n${test}\ntest()",
"juniper": {
"repo": "ines/spacy-course",
"branch": "binder",
Expand Down

0 comments on commit e148eb1

Please sign in to comment.