I am trying test driven development for the first time (test first development, actually). I wrote down my specifications, then alternated writing tests, then code, writing the code to pass the latest test and not break prior tests. My code is doing input validation on a user-supplied file path:
- Does the path exist and is it a file?
- Is the file in a specific format?
- Does the file contain a specific field?
- Does the file have a feature where the field is set to a given value?
This led me to write functions that return True/False for each condition, and tests for inputs leading to True and False outputs. However, there is some duplication between the functions (loading the file, etc.) and I could write a more streamlined function that combines all the checking. This is important in my case because the files can be large.
Where I'm having issues:
- Do I also refactor the tests?
- If I have a single larger function that outputs True/False based on the sub-checks, how can I still test for the individual specific conditions?
- Should I instead raise Exceptions, and check that the correct exceptions are raised?
check_file_validity
, and those tests need to be specific enough to cover all the individual conditions in my smaller tests, without one hiding another.