I am starting a new project and wish to do it from beginning to end using TDD best practices. This is also my practical foray for the first time into OOP. The application will have a primitive editor to create and edit certain text files that the program will need. The first file the program will need is one to be called classifiers.txt, which will be a list of grammar abbreviations that will be used while classifying sentences. The beginning of such a file might look like:
N # Noun V # Verb ADJ # Adjective So I started out with the following test_main.py (Later I will factor out code into separate modules once I know what will make sense. For now I am starting out with a single program file, main.py.): #!/usr/bin/env python3 import unittest import grammar.main class TestEditor(unittest.TestCase): """ Test all methods of the Editor class. """ def setUp(self): self.editor = grammar.main.Editor() def test_edit_classifiers(self): # ??? if __name__ == "__main__": unittest.main() And the current code in main.py: #!/usr/bin/env python3 class Editor(object): def __init__(self): pass def edit_classifiers(self): pass if __name__ == "__main__": main() At this moment I am stumped as to what sort of test to write for the method edit_classifiers(). At this very beginning stage, what I would want to have happen is to call edit_classifiers, it will try to open classifiers.txt, realize that it does not exist yet, and offer to create this file for the user to start entering the needed grammar classifiers. I don't want to mess with what will become the program's *real* classifiers.txt (And other needed text files to come, that will likewise be editable.), so how do I simulate these various needed file operations in a way that tests the actual program code, but without touching the actual data files? I was looking at "How to Mock Python's `open` function" at http://www.ichimonji10.name/blog/6/, which looks potentially promising, but looks complicated. This leads me to look at https://docs.python.org/3.4/library/unittest.mock.html, but right now I am finding this heavy going, and it will probably take me some time and further research before I feel I am really understand applying this mock process to file ops. But is this direction I need to investigate? TIA! -- boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor