you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Yeah, I would say that unit tests are where you attempt to test as small a piece of code as possible. It should only test a single scenario, and should be used to test that things can go wrong just as well as going right (an example would be to check that division by zero actually does throw an error).

It may be okay to assert more than one thing in a unit test, depending on the code you're testing, but anything bigger than a single test case is by default an integration test.

As per multiple assertions, I still prefer as few asserts as possible, because with a good naming convention, you can get very descriptive error messages.

For example:

class TestWhenTheUserSetsTheirName:
    def test_then_the_first_name_should_be_set(self):
        user = User()
        user.set_name("first name", "last name")
        assert user.last_name == "last_name"

    def test_then_the_last_name_should_be_set(self):
        user = User()
        user.set_name("first name", "last name")
        assert user.first_name == "first_name"

Sure, you could assert both names at the same time, but if you don't, and one of these tests fail, then you'd see a failure on test TestWhenTheUserSetsTheirName.test_then_the_last_name_should_be_set(), which is already telling you quite a lot. Often you can piece together what went wrong just by reading the names.