ines / course-starter-python

👩‍🏫🐍 Starter repo for building interactive Python courses
https://course-starter-python.netlify.com
MIT License
504 stars 118 forks source link

Add local testing capability #12

Open rahuldave opened 5 years ago

rahuldave commented 5 years ago

I was looking to test python code locally. There is a conftest.py in the spacy course which could be used as a start, but the testing in this repo does not have that support. So i created a file which has the following hacky code

from wasabi import Printer
import sys

with(open(sys.argv[1])) as fd:
    thesolution = fd.read()
with(open(sys.argv[2])) as fd:
    thetest = fd.read()

__msg__ = Printer()
__solution__ = """{}""".format(thesolution)

exec(thesolution)

exec(thetest)

print(globals().keys())
try:
    test()
except AssertionError as e:
    __msg__.fail(e)

The advantage of this repo's mechanism is that only wasabi needs to be there on the binder server for testing. But if we want to test with data and then need to put the full repo on binder (rather than just a shadow binder repo/branch with maybe a data folder), then additional stuff for pytest does not seem a large imposition?

rahuldave commented 5 years ago

One additional thought: pylint/flake8 the student submission. Then "in solution" type tests need not worry about whitespace and stuff.

ines commented 5 years ago

Not 100% sure I understand the question – but you should be able to port over the conftest.py from my spaCy course repo and then run pytest. The conftest will take care of putting together the tests using the solution code and test file, and run them accordingly.

One additional thought: pylint/flake8 the student submission. Then "in solution" type tests need not worry about whitespace and stuff.

Yes, I'd use black for that: https://github.com/python/black