Created
October 31, 2019 12:13
-
-
Save karlcow/4c849caa17857c1444bb67b34ecc99cd to your computer and use it in GitHub Desktop.
Trying to create an example of doctests for testing a function. Developed in the spirit of TDD.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import doctest | |
import datetime | |
def post_date(content): | |
""" | |
Extract the date from the content | |
>>> post_date('Date: 2019-10-31') | |
'2019-10-31' | |
>>> post_date('Date: foobar') | |
Traceback (most recent call last): | |
... | |
ValueError: time data 'foobar' does not match format '%Y-%m-%d' | |
""" | |
date = content.split(':')[1].strip() | |
try: | |
datetime.datetime.strptime(date, '%Y-%m-%d') | |
return date | |
except Exception as error: | |
raise error | |
if __name__ == "__main__": | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So before the function was implemented, I just had tests as I do with unittests for TDD.
Define the tests
and running the tests with
python doctest_tdd.py
would output:Implement parsing
Then running the tests.
Implement the format checker
Lool at the final code above.
Let's run the tests one last time.
Nothing. All tests have passed.
Verbose?
If we want to have more details:
python doctest_tdd.py -v