Buy

TDD & Unit, Integration & Functional Tests

Unit, Functional & Integration Tests

Before we get back to coding... we need to talk about just a little bit of theory! No, don't run away! Give me just 2-ish minutes!

So, there are actually three different types of tests, and we're going to try them all. The first is a unit test - that's what we've created. In a unit test, you test one specific function on a class. It's the purest way to test: you call a function with some input, and test the return value. We'll learn more about this later, but each unit test is done in isolation. If, for example, a class needs a database connection, we're actually going to fake that database connection so that we can focus on testing just the logic of the class itself.

The second type of test is an integration test... or at least, that's my name for it. An integration test is basically a unit test: you call functions and check their return values. But now, instead of faking the database connection, you'll use the real database connection. We'll talk about when and why this is useful later.

The third type of test is a functional test. In our world, this basically means that you're writing a test where you programmatically command a browser. Yep, you literally write PHP code that tells a browser to go to a page, click a link, fill out a form, click submit, and then assert that some text appears on the next page.

More on all of these things later.

How much to Test?

Another question is how much you should test. Does every function need a unit test? Does every page and every validation error of every form need a functional test? Absolutely not! That sounds worse than a raptor claw across a chalkboard!

Especially if you're new to testing, don't worry: a few tests is way better than none. And honestly, I think many people create too many tests. I follow a simple rule: if it scares me, I test it. Too many tests take extra time, add little value, and slow you down later when they fail after you've made a minor change.

In our app, we've tested the getLength() and setLength() methods. These do not scare me. In the real world, I would not test them.

Test-Driven Development

A few minutes ago, I mentioned the term "Test-Driven Development" or TDD. TDD breaks coding into three steps. First, create the test. Second, write the minimum amount of code to get that test to pass. And third, now that your tests are passing, you can safely refactor your code to make it fancier.

So, test, code, refactor: these are the steps we're going to follow. Do I always use TDD in my real projects? Um... yes!? No, not really: I'm not a purist. Heck, sometimes you don't even need a test! Gasp! But yea, usually, if I plan to write a test for a new feature I'm working on I'll follow TDD.

Oh, and TDD is about more than just making sure you have a lot of tests. As you'll see, it forces you to think about how you want to design your code.

Enough theory! Let's try it already!

Leave a comment!

  • 2018-05-20 Coder

    Interesting to hear for me also. I was also really annoyed when I saw in one company when people write bad code, and without tests, but they have more experience, so I assumed they should write tests, or they should be valued less than me. But they were valued a lot by director, and I was doing poor because of low productivity. I did not attend all the classes in my univercity, but I believe there was no productivity classes, agile, or similar stuff classes. One thing there was - a psychology, which I probably should have not skipped as much as I did, maybe would have learned something useful, because psychology is also big part of success I believe.
    This really should be teached in all univercities and colleges to not get so much bad experience. I was even losing trust, if I can have good life ever with strugling so much at work. But I did not give up, because I did not want to do most of other jobs. And also if I would want some other, I did not have a skill.

  • 2018-05-14 weaverryan

    Hey Coder!

    You share my experience exactly, and I think you need to go through this process to really "feel" the pros and cons of testing. I'm very happy to hear this! And yea, there are certainly people (smart people, smarter than me!) that disagree. But in the real world, with budgets, goals, deadlines, etc - you *must* find your balance.

    Anyways, thanks for the message - it's great to hear other experiences that mirror my own!

    Cheers!

  • 2018-05-13 Coder

    good idea about - if it scares you - test. I have worked in a company where they tried to cover with unit tests everything. And I thought I am better programmer because I tried also. I tried even test every line of code of integration tests but and during code reviews I tried to encourage team also to test same as me that really used to take lot of time, so I guess now if I would go back to that company, I would just test as much as company requires and if I feel code is scary and probably would be better that way. The problem is that I have read that robert martin saying you need to cover asymptotically 100 % of the code. Maybe he should edit this book and also publicly tell to those who own the book - dont do it, it takes too much time.

    Plus - I have tried TDD. But TDD also tells - before writing code - write a test. That meant that I cannot write a code without writing test, and so it leads to 100 % coverage. TDD made me slow down hell lot, and after couple of months the teamlead said to not do TDD and I think he was right and made me faster again. TDD maybe works for simple functions as in the video, but for real complicated stuff - I do no think it always is good. I stopped seeing a benefit of it, especially after learned that no need 100 % of coverage.

  • 2018-03-20 Diego Aguiar

    Hey Thao Truong

    The subtitles are fixed now, if you still see them de-synced, you may have to clear your browser cache for this page

    Thanks!

  • 2018-03-20 weaverryan

    Hmm, thanks Thao Truong! It looks like something got de-synced. We'll take a look and fix it!

    Cheers!

  • 2018-03-19 Thao Truong

    The subtitles don't match what's being said.