• 20 hours
  • Medium

Free online content available in this course.



Got it!

Last updated on 4/27/23

Benefits of using tests

Now that you have a better idea of ​​what tests are in general and what unit tests are in particular, we'll be able to address the main question of this course:

Why write unit tests?

There are many reasons. Here are the top 5!

1. Prove that your code works βœ…

By definition of a unit test, all the code that your unit tests cover works. So, having unit tests is in itself a guarantee that the code works!

If you've already had a chance to navigate a large code base, you've probably experienced feeling dizzy and being afraid to touch a line in fear of breaking something. That's because you could not anticipate the implications of your changes.

With unit tests, if a change breaks something, you'll see it right away, because one or more of your tests will turn red!

2. Prevent bugs and regression 🐞

Because unit tests prove that your code works, they prevent bugs from occurring. And as everything is tested, you can change your code base without fear of regression; the tests will immediately indicate a problem should one occur!

3. Require well-designed classes πŸ€”

Unit tests function at class interface level. So to test easily, you'll have to think through all the elements of your code - you are basically forced to design your code well.

4. Form a living documentation of your code πŸ“š

Each test will cover a use case of one of your classes. So if all your code is tested, your tests can serve as the most current functional documentation of your program. They describe everything your code does!

Look at a test code snippet of our Game class that we've just tested:

func testGivenGameIsAtStart_WhenAdvanced_ThenCardShouldBeMarkedWithXAndNextPlayerTwo() {}
func testGivenGameIsAtStart_WhenAdvancedWithCrossesInRowOne_ThenGameShouldBeOverWithWinnerAsPlayerOneAndWinningIndicesNotNil() {}
func testGivenGameIsAtStart_WhenAdvancedWithZerosInRowOne_ThenGameShouldBeOverWithWinnerAsPlayerTwoAndWinningIndicesNotNil() {}
func testGivenGameIsAtStart_WhenAdvancedWithCrossesInColumnOne_ThenWinnerShouldBeNotNil() {}
func testGivenGameIsAtStart_WhenAdvancedWithCrossesInDiagonalOne_ThenWinnerShouldBeNotNil() {}
func testGivenGameIsAtStart_WhenAdvancedWithCrossesInDiagonalTwo_ThenWinnerShouldBeNotNil() {}
func testGivenGameIsAtStart_WhenAdvancedToFill_ThenGameShouldBeOverWithWinnerAsNilAndWinningIndicesAsNil() {}

We can recognize 4 use cases of the Game class!

Unlike  traditional documentation written outside of the code, it's guaranteed to always stay up to date! Indeed, when the code needs a modification, so do the tests!

5. Make you a professional πŸ’Ό

Finally, when you write tests, you create code that's easy to maintain, prevents bugs, and surely works. These are the essential components of work professionally done!

If a plumber comes to your house to repair a leak and tells you that the repair may stop the leak, you'll call them a fraud! Well, in development it's the same - you have to guarantee the quality of your work and a good test suite will allow you to do that!

So, write some nice tests and be proud to put your name on your work! 😎

Let's Recap!

  • Writing unit tests must become a reflex because it's a must if you want to become a professional iOS developer!

  • There are good reasons for implementing unit tests:

    • Prove that your code works

    • Prevent bugs and regression

    • Require well-designed classes

    • Form a living documentation of your code

    • Make you a professional

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement