7. August 2008, 18:42, by Maarten Manders

Static + Unit Tests = Arrrghhh!

All I needed to do was renaming Trevi_Auth to Tilllate_Auth. Sure, it affected some 41 files, but that’s nothing a little bit of search & replace can’t do. The big headache started when I ran the unit tests.

Shuffle Unit Tests = Fail.

It’s absolutely amazing how much you can mess up unit tests just by changing their order! (Trevi_* comes after Tilllate_*) Everyone knows that tests are supposed to be independent. But we all know how it is. People get lazy, mocks are complicated and testing is boring anyway. And don’t forget about that cool pattern called “Singleton” that we’ve all read about in an inflight magazine!

Punish Me!

Obviously we can’t do testing right without getting beaten with a stick. So where’s that stick? Is there a way to reset all static variables in PHP? Is there a PHPUnit hack that forks a separate PHP process for each test?

What is your solution to keep your tests from sticking together like spit ? I’m happy to learn about it!

11 Comments

  1. phpt :)

    Comment by Lukas — 7. August 2008 @ 20:24

  2. Don’t use Singletons, especially static Singletons … or static anything else … you always end up removing them anyway, so why start.

    Comment by Christopher — 7. August 2008 @ 22:10

  3. Agreed with Lukas. Either that, or REFACTOR!

    Comment by Edward Z. Yang — 7. August 2008 @ 23:28

  4. 1.) Have a continuous integration solution in place. This is the reference where the tests have to pass. “Works for me” is no longer an excuse than.
    2.) Use the –repeat Option and run the test suite twice in the same context. This will help you to fix *a lot* of setup/teardown issues.
    3.) Don’t use singletons, use a service locator and dependency injection.
    4.) Mock and stub the pain away.
    5.) Various (motivating) punishments come in my mind, although I never implemented one :)

    Comment by Lars Strojny — 7. August 2008 @ 23:39

  5. I find writing tests are one of the challenging part of programming and not boring at all! It’s the moment where you set fails that you are going to fight afterwards with your code!

    Never had much problem with tests dependencies. A good use of setUp and teardDown and testing only one function (even one use case of a function) per test really makes it.

    Good luck ;)

    Comment by lejoe — 7. August 2008 @ 23:59

  6. So first you ask for a stick, and in the next sentence you ask for a way to hide from it? Doesn’t make much sense to me.

    Your tests are speaking to you. They are saying “Please don’t use global variables, you are hurting us”. Listen to them.

    Comment by troelskn — 8. August 2008 @ 00:04

  7. And you can beat your singleton with this : http://paste.pocoo.org/show/81547/ and use $forcereload for tests

    Comment by lejoe — 8. August 2008 @ 00:07

  8. There is a branch in PHPUnit’s Subversion repository that runs each test in a separate PHP process. This branch will be merged for either PHPUnit 3.4 or PHPUnit 4.0.

    You can also follow Lukas’ advice and write your tests that exercise static methods as PHPT — and run those through PHPUnit. PHPUnit’s own test suite is a good example of a mix between PHPUnit and PHPT tests.

    Comment by Sebastian Bergmann — 8. August 2008 @ 00:51

  9. Force other people to read about:

    http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/

    and

    http://misko.hevery.com/2008/07/30/top-10-things-which-make-your-code-hard-to-test/

    Apply stick liberally.

    Comment by Daniel O'Connor — 8. August 2008 @ 06:02

  10. [...] a hint from Maarten Manders when renaming and moving around your unit testing order: It’s [...]

    Pingback by Maarten Manders’ Blog: Static + Unit Tests = Arrrghhh! | Development Blog With Code Updates : Developercast.com — 8. August 2008 @ 23:26

  11. And patrice suggested me today to read this one : http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/

    Really simple to understand and really helpfull

    Comment by lejoe — 19. August 2008 @ 18:55

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

© 2014 tilllate Schweiz AG - Powered by WordPress