1

There is a class named State that has several attributes, which I need to write unit tests for. The test will need to have certain actions that change attribute values of the State instance. Expected parameters are located inside of the dictionary. The unit test will compare the attributes of the State instance with the values of the dictionary for equality.

The question is what is the best place to keep the comparison logic at? I was thinking about 2 options:

  1. Add __eq__ method to the State class that contains comparison logic.
  2. Add helper function inside of the test module that contains comparison logic.

Which one of the options is better and why?

1

Both ways are fine.

I would recommend if you imagined how things would be if you were writing the State class driven by the tests, rather than having the tests afterward: Which option would you pick? Would you design the class so that it clients would be accessing its attributes, rather than sending messages through its methods?

If the answers are not obvious, re-writting State driven by tests may give you insights about how to better design it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.