7. Testing your code.
Lecture notes
- Write tests with both pytest and doctest for the practical code from weeks 5 and 6.
- For
IsReduplicated
- For
find_redup
- For
strip_gutenberg
- For
process_line
- Hint
- for the doctest, write the test, and look at the failure
message to see the expected output, use this in the text
- for a multi-line string in the test, you need to escape the newlines
so "line one\\nline two"
instead of "line one\nline two
- Write code to analyze a text in terms of characters
Return a dictionary with the count of
- all characters (length)
- ascii (unaccented characters - hint use
isascii()
and isalpha()
)
- accented characters
- vowels
- consonants
- Document this using docstring or pytest
- test with some Czech
- test with some Pinyin
- test with some Vietnamese
- Write a
swear_filter
function that censors
offensive words by replacing them with asterisks (e.g., "darn" ->
"****").
You should cover at least Carlin's seven dirty words:
shit, piss, fuck, cunt, cocksucker, motherfucker, tits
- Write pytest tests for
swear_filter
- Verify correct censoring of offensive
words in various contexts.
- Add a test for words within other words --- we will learn how to pass this next week
test driven development write tests, and then makes them pass
- Add doctest examples showing censored versions of sentences
with sample swear words.
- ❂ Extend the function and tests to optionally
- replace all letters except 1st and last: shit → s**t
- replace short words with bleep and long words with bleepbleep
❂ If you haved finished everything else, try this
Summary
- Learned how to write tests for functions and classes using the
pytest
and doctest
modules.
- Practiced writing test functions to verify specific behaviors of
functions.
- Explored using fixtures to efficiently create reusable resources
for multiple tests.
- Testing helps ensure that critical behaviors of functions and
classes actually work.
- You should add tests on critical code behaviors, without aiming
for full coverage in early projects.
- Tests provide confidence in maintaining and improving code
without breaking existing functionality.
- Initial tests make projects more respected and encourage
collaboration with other programmers.
- Responding to failed tests is easier and faster than handling
bug reports from users.
- Contributions to collaborative projects often require passing
existing tests and adding new ones for any new functionality.
- You should at least have doctests for your assignment
LAC: Language and the Computer Francis
Bond.