Effective Unit Testing?



  • Can anyone recommend an accessible but thorough, real-world (i.e. not oversimplified examples) resource about writing and using unit tests effectively without an existing testing framework?  I'd like to introduce the concept to a coder who's clearly never heard of them before, and I'd probably use them more myself if I had a better intro.

    We're currently in a non-object-oriented PHP environment, so something that applies to scripting languages instead of compiled languages, and to procedural rather than object-oriented techniques, wouldn't hurt either. 

    Many thanks!
     



  • I have not yet familiarized myself with unit testing, but I am wondering, if you need to write code to test your code, then who will write the code to test your test-code???



  • You can work in a team and split up the testing / implementing, but that's a bit extreme. Normally you just write your own test cases, that's never been a problem for me. Knowing the code I also know where possible problems may occure and I make sure that there are tests in place to make sure they all work correctly.

    There are a couple of ways to use unit tests. One is for test driven design, i.e. writing the tests before the code. You then add the code until all tests pass. Add more test cases along the way as you see possible problems arise.

    Another big reason for unit tests are not so much for the code you're writing right now, but for later modifications. When someone comes along who's no longer familiar with all the details and makes adjustments to add new features, solid unit tests make sure that no existing functionality is broken.

     

    As for the original post, testing objects is much easier so I think it's going to be tough to add unit tests to php when you don't have any single units to test.
     



  • @Nandurius said:

    As for the original post, testing objects is much easier so I think it's going to be tough to add unit tests to php when you don't have any single units to test.

    If the code is not a big heap of spagetti, but structured into not reasonable sized procedures, unit testing should be possible just as well. 



  • @Ice^^Heat said:

    I have not yet familiarized myself with unit testing, but I am wondering, if you need to write code to test your code, then who will write the code to test your test-code???

    The original code tests the unit test. If the unit test fails, the bug can be either in the tested unit or the unit test code.

    Of course there is a chance that both pieces of code err in the same way, so the unit test will not fail. 



  • @Ice^^Heat said:

    I have not yet familiarized myself with unit testing, but I am wondering, if you need to write code to test your code, then who will write the code to test your test-code???

    You're destroying my happy little agile, documentation-free world!

    How can the tests and the code equal the documentation if the tests are flawed?

    /me runs for the hills.



  • @Whiskey Tango Foxtrot? Over. said:

    You're destroying my happy little agile, documentation-free world!

    How can the tests and the code equal the documentation if the tests are flawed?

    /me runs for the hills.

    On the other hand, how can you ever be sure that elaborate documentation is correct? 





  • @ammoQ said:

    On the other hand, how can you ever be sure that elaborate documentation is correct? 

    Who said anything about elaborate? As long as there's *something* that says "this is what this is, this is how it works", or at the very least "this is my intent", I'm happy.

     

    Unfortunately, I live in an agonizingly stupid world where "documentation = code + units"



  • @ammoQ said:

    @Whiskey Tango Foxtrot? Over. said:

    You're destroying my happy little agile, documentation-free world!

    How can the tests and the code equal the documentation if the tests are flawed?

    /me runs for the hills.

    On the other hand, how can you ever be sure that elaborate documentation is correct? 

    We just hold that truth to be self-evident.


Log in to reply