Skip to content

TestDrivenDevelopment (TDD) for `pyamidict`

petermr edited this page Sep 28, 2021 · 3 revisions

pyamidict TDD

  • goal: to create community code from scratch for pyamidict, a tool for creating and validating dictionaries.
  • software: pytest

design

The key design is to think of everything that the system would need to do.

Think of a test (maybe a rule). Then create a test for it (which will fail because the software isn't written). Then write just enough code (even if seemingly silly) to make the test pass. Repeat.

Thats it. WE (@shweata and @anuv) went round the cycle about 20 times.

example

Create a test

    def test_dictionary_exists(self):
        assert self.dict1.exists(), f"file should exist {self.dict1}"

run it.

Test fails. Create the an empty file ("py4ami/resources/amidicts/dict1.xml"):

        self.dict1 = Path(Path(__file__).parent.parent, "py4ami/resources/amidicts/dict1.xml")

run it

Test passes!

Add another test

... to see if it contains XML

    def test_dict_contains_xml_element(self):
        root = etree.parse(str(self.dict1))
        assert root is not None

Test fails!

edit the file (dict1.xml):

<dictionary>
</dictionary>

Test passes.

add another test

... to validate the name of the root element.

    def test_dict_has_root_dictionary(self):
        root = etree.parse(str(self.dict1)).getroot()
        assert root.tag == "dictionary"

Test passes

add another test

... to see if dictionary has a title

    def test_dict_has_XML_title(self):
        root = etree.parse(str(self.dict1)).getroot()
        assert root.attrib["title"] == "dict1"

test fails

edit the XML file

<dictionary title="dict1">
</dictionary>

Test passes

add another test

... to see if filename is correct

    def test_dict_title_matches_stem_of_filename(self):
        root = etree.parse(str(self.dict1)).getroot()
        assert root.attrib["title"] == self.dict1.stem

Test passes

Guidelines

  • Work in very small steps.

  • Do not give files or methods a body until forced to do so.

Notes

We haven't got the pytest setup and teardown included yet

Clone this wiki locally