mardi 24 janvier 2017

Testing methods that depend on each other

Let's say I have a simple data-structure Store with two methods: add and list_all (Example in python):

class Store:
    def __init__(self):
        self.data = []
    def add(self, item):
        self.data.append(item)
    def list_all(self):
        return list(self.data)

Testing its methods would look something like:

def test_add():
    store = Store()
    store.add("item1")
    items = store.list_all() 
    assert len(items) == 1
    assert items[0] == "item1"

def test_list_all():
    store = Store()
    store.add("item1")
    items = store.list_all() 
    assert len(items) == 1
    assert items[0] == "item1"

Well these tests are awkward, they have literally the same body. To test the list_all method, I have to assume that add already works correctly, and to test add I have to use list_all to check the state of the Store. How do you test these kind of methods? Do you just write a single test case and say "this proves that both methods work fine"?

PS: It's a theoretical question. I am working on testing a complex system, and couldn't find where to start a bottom-up approach, because of such problems.

Aucun commentaire:

Enregistrer un commentaire