Skip to content

Instantly share code, notes, and snippets.

@Pen-y-Fan
Last active September 11, 2024 23:33
Show Gist options
  • Save Pen-y-Fan/5a56a255a4733a04e10c58176cbe5f57 to your computer and use it in GitHub Desktop.
Save Pen-y-Fan/5a56a255a4733a04e10c58176cbe5f57 to your computer and use it in GitHub Desktop.
Test Driven Development (TDD)

Learning Plan for Test Driven Development (TDD)

These learning resources primarily focus on Test Driven Development (TDD).

  • There is an emphasis on learning using PHP, Laravel and PHPUnit.
  • All these resources are free (at the time of writing)

Table of Contents

1. Videos

2. Blog Posts

3. Books, manuals and websites

4. Tooling

  • VS Code
    • Extension: Better PHPUnit (video)
      • Configure CTRL + T to run tests, CTRL SHIFT + T to rerun
  • PHPUnit (doc 8.0)
    • Recommended to install per project:
      • composer require --dev phpunit/phpunit ^|version|
      • e.g. composer require --dev phpunit/phpunit ^8
      • Included with dev dependencies on the laravel framework.
    • Install globally, configure phpunit.bat and add to path (not recommended but useful for running from the command line).

5. Three Laws of TDD

Over the years I have come to describe Test Driven Development in terms of three simple rules. They are:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Uncle Bob's full article: Source

6. Examples & Code Kata

7. Project With Tests

  • October CMS - Based on Laravel, on setup there is a full set of tests to run. (Runtime ~10 min - with xdebug on), turn xdebug off if not generating reports
    • October has been installed and run, examine the tests in more detail, what are they testing, etc.
  • Laravel Boilerplate - Tests take 10 min to run (with xdebug on), turn xdebug off if not generating reports
    • Laravel Boilerplate has been installed and run, examine the tests in more detail, what are they testing, etc.

8. Conclusion

Notes are still ongoing and may not necessarily make sense out of context.

  • TDD, when used correctly are good.
  • Documentation can be created from well named tests.
  • Test behaviour not implementations.
  • Testing is an art, there isn't a right way for everything.
  • Testing takes practice
  • Try to use real objects rather than mock data.
  • Tests should be trustworthy.
  • Anna Filina (Recap)
    • Write a test to prove what is being tested works.
    • Write tests when you see a bug
    • Write test when you improve code
    • Write tests as you write new code
    • Write tests before you write code
    • Text unexpected scenarios
    • Testing = Pre-emptive Debugging
  • Google
    • Small - like unit testing
    • Medium
    • Large - acceptance testing
  • Mocking can cause dependencies or coupling.
  • Ensure that both success and failure cases are covered by appropriate unit tests
  • There's a lot of value in reading books on good coding and design practices even if they don't focus on unit testing
  • TDD is recommended for any long lasting project which may need new features in the future.

9. Other Learning Plans

The next plan from TDD was Learning Plan for Design Patterns and Principles of Good Design

10. License

Copyright (c) 2019 Pen-Y-Fan

Permission is hereby granted, free of charge, to any person obtaining a copy of this learning plan and associated documentation files (the "Plan"), to deal in the Plan without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Plan, and to permit persons to whom the Plan is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Plan.

Content copied from Wikipedia.org is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply.

@lastrafda
Copy link

Great info!

@rask
Copy link

rask commented Sep 3, 2020

Hey this is nice, thanks.

@curder
Copy link

curder commented Mar 25, 2021

Thanks for your sharing!

@pauloboss
Copy link

Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment