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?

asked Mar 7, 2021 at 22:31

1 Answer 1

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.

answered Mar 8, 2021 at 10:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.