Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Test First , Test Last or Test At All?

Test First , Test Last or Test At All?

Scheduled Pinned Locked Moved The Lounge
wpftestingbeta-testingquestionlounge
36 Posts 18 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T TheIdleProgrammer

    Hi everyone, I'm curious about other peoples views on unit testing. The company I work for is quite small and there are only four full time developers. We all have different views on programming styles ranging from very old school, constantly questioning the need for object orientation, through to very modern, arguing for progress and trying to keep everybody up to date. We adopted unit testing around two years ago and, although the overall development process takes a little longer, I think everyone agrees that it's more than proved its worth. In general we have written our unit tests after we have written our code but, for me, it has never really seemed like the correct or logical way to do it so recently I have been trying test-driven development and I have found it to be quite useful. I guess really I am keen to know what other people think about the whole approach to unit testing.

    D Offline
    D Offline
    Duncan Edwards Jones
    wrote on last edited by
    #13

    Test immediately after writing (I should do test first but I find running a test to fail because I haven't written any code yet is a bit of a mind set change) However - how many contributions to Codeproject include the unit tests? maybe that should be a suggestion?

    '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

    A 1 Reply Last reply
    0
    • G Graham Bradshaw

      What do you do for the functions you can't write tests for (i.e. ones that depend on specific external conditions?). For example, imagine a function that took a URL, fetched the page, and returned the contents of the page. How would you unit test that?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #14

      In those cases, you would write a Mock object that returned a facsimile of the output. Mocking is great because it allows you to break the heavy coupling that typically goes into a project.

      Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

      G 1 Reply Last reply
      0
      • P Pete OHanlon

        In those cases, you would write a Mock object that returned a facsimile of the output. Mocking is great because it allows you to break the heavy coupling that typically goes into a project.

        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

        G Offline
        G Offline
        Graham Bradshaw
        wrote on last edited by
        #15

        The original code (let's assume) makes a request over the network. Are you suggesting removing that code to do the unit test?

        P 1 Reply Last reply
        0
        • G Graham Bradshaw

          What do you do for the functions you can't write tests for (i.e. ones that depend on specific external conditions?). For example, imagine a function that took a URL, fetched the page, and returned the contents of the page. How would you unit test that?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #16

          If you know what the page should look like you could simply fetch it into a string and compare it. I have a bunch of shared code that, amongst other things, includes some cross-platform socket classes which I test in a similar way. I supply a special config file to the unit test framework that contains host names, etc. that I need when the test runs. For example, I have a class that logs into a POP3 server and checks the mail count and the mail server/user/password details are supplied in this config file. Some frameworks also allow you to create 'mock objects' - for example a 'pretend' HTTP stream that you could feed into your test code, etc. The Boost stuff I use isn't that advanced, but I believe that mock object frameworks are available for C#/Java/etc.


          Kicking squealing Gucci little piggy.
          The Rob Blog

          G 1 Reply Last reply
          0
          • T TheIdleProgrammer

            Hi everyone, I'm curious about other peoples views on unit testing. The company I work for is quite small and there are only four full time developers. We all have different views on programming styles ranging from very old school, constantly questioning the need for object orientation, through to very modern, arguing for progress and trying to keep everybody up to date. We adopted unit testing around two years ago and, although the overall development process takes a little longer, I think everyone agrees that it's more than proved its worth. In general we have written our unit tests after we have written our code but, for me, it has never really seemed like the correct or logical way to do it so recently I have been trying test-driven development and I have found it to be quite useful. I guess really I am keen to know what other people think about the whole approach to unit testing.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #17

            To be honest - while there are some issues with writing Unit Tests up front, I am now a big fan of TDD. Having started using it about 2 years ago, I find that I now automatically think in that direction. However, a word of warning for those who write Unit Tests. It is vitally important that you think about the tests that you apply to your code. It's all very well having unit tests, but if they don't exercise the code you are testing or they leave large parts untouched, the unit tests are worthless. Unit Testing is easy - being able to write good unit tests is a much more difficult beast.

            Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

            L 1 Reply Last reply
            0
            • G Graham Bradshaw

              The original code (let's assume) makes a request over the network. Are you suggesting removing that code to do the unit test?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #18

              Not at all, although I wouldn't normally attempt to retrofit Unit Tests into code. The way I normally work is to develop loosely-coupled code, where I use interfaces to define what needs to be done. Then, I mock up the objects based on the interfaces. Take the classic example of wanting to update a database. In order to have repeatable tests, I would create an interface for the database update - this would then be mocked to simulate the update. I would then run my unit tests against this mock. Lo and behold, I've got code that I can retest.

              Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

              G 1 Reply Last reply
              0
              • P Pete OHanlon

                To be honest - while there are some issues with writing Unit Tests up front, I am now a big fan of TDD. Having started using it about 2 years ago, I find that I now automatically think in that direction. However, a word of warning for those who write Unit Tests. It is vitally important that you think about the tests that you apply to your code. It's all very well having unit tests, but if they don't exercise the code you are testing or they leave large parts untouched, the unit tests are worthless. Unit Testing is easy - being able to write good unit tests is a much more difficult beast.

                Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #19

                Indeed, and obtaining anywhere close to full coverage is no mean feat!


                Kicking squealing Gucci little piggy.
                The Rob Blog

                P 1 Reply Last reply
                0
                • P Pete OHanlon

                  Not at all, although I wouldn't normally attempt to retrofit Unit Tests into code. The way I normally work is to develop loosely-coupled code, where I use interfaces to define what needs to be done. Then, I mock up the objects based on the interfaces. Take the classic example of wanting to update a database. In order to have repeatable tests, I would create an interface for the database update - this would then be mocked to simulate the update. I would then run my unit tests against this mock. Lo and behold, I've got code that I can retest.

                  Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                  G Offline
                  G Offline
                  Graham Bradshaw
                  wrote on last edited by
                  #20

                  So how do you unit test the network access code in my example?

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    If you know what the page should look like you could simply fetch it into a string and compare it. I have a bunch of shared code that, amongst other things, includes some cross-platform socket classes which I test in a similar way. I supply a special config file to the unit test framework that contains host names, etc. that I need when the test runs. For example, I have a class that logs into a POP3 server and checks the mail count and the mail server/user/password details are supplied in this config file. Some frameworks also allow you to create 'mock objects' - for example a 'pretend' HTTP stream that you could feed into your test code, etc. The Boost stuff I use isn't that advanced, but I believe that mock object frameworks are available for C#/Java/etc.


                    Kicking squealing Gucci little piggy.
                    The Rob Blog

                    G Offline
                    G Offline
                    Graham Bradshaw
                    wrote on last edited by
                    #21

                    So you're not testing the network access code, just bypassing it, and putting fake results in the output?

                    L 1 Reply Last reply
                    0
                    • G Graham Bradshaw

                      So you're not testing the network access code, just bypassing it, and putting fake results in the output?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #22

                      In my case no, as I am not using mock objects. My code is actually connecting to a known POP3 server over the network, logging in using a known user/password, etc. and all the responses I expect to the POP3 commands are pretty much fixed. For example, when I connect to the POP3 server I use, I know the response will be something like:

                      +OK Qpopper (version x.x.x) at HOST starting.

                      When the code I'm testing issues a USER command, I know the response should be:

                      +OK Password required for USER.

                      etc. etc. The important thing is to break your code up into small, easily tested functions rather than trying to test monolithic code that consists of thousands of lines. This makes life much, much easier.


                      Kicking squealing Gucci little piggy.
                      The Rob Blog

                      1 Reply Last reply
                      0
                      • G Graham Bradshaw

                        So how do you unit test the network access code in my example?

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #23

                        If we follow the TDD approach, then I would mock this up so that the Mock object represented the network access. In other words, I might have an interface called INetworkAccess which I mocked up to represent the access. My test would then test the object which relied on INetworkAccess which would verify whether or not this code worked independent of the network access. Now, you will also need to test your physical network access code, so I would also mock up an object that you could guarantee would be there 100% of the test time. Your network access code would then be tested against this object. By doing all of this, you've broken the testing apart so that you can test code in isolation without having to rely on outside factors. I would really suggest that you take a look at frameworks such as RhinoMocks and areas such as Test Driven Development. They do take some getting used to, but once you are used to them you will find that they really do help.

                        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Indeed, and obtaining anywhere close to full coverage is no mean feat!


                          Kicking squealing Gucci little piggy.
                          The Rob Blog

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #24

                          Rob Caldecott wrote:

                          Indeed, and obtaining anywhere close to full coverage is no mean feat!

                          Sometimes you have to accept that you can't get 100%. You can become obsessed with generating absurd test cases just to get to a line of code, the development loses focus and the project slips.

                          Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                          L 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            Rob Caldecott wrote:

                            Indeed, and obtaining anywhere close to full coverage is no mean feat!

                            Sometimes you have to accept that you can't get 100%. You can become obsessed with generating absurd test cases just to get to a line of code, the development loses focus and the project slips.

                            Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #25

                            Indeed. What do you do about private functions?


                            Kicking squealing Gucci little piggy.
                            The Rob Blog

                            P 1 Reply Last reply
                            0
                            • L Lost User

                              Indeed. What do you do about private functions?


                              Kicking squealing Gucci little piggy.
                              The Rob Blog

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #26

                              Well - a private function must be there to satisfy a purpose. It shouldn't exist in isolation. Your testing of the exposed surface should attempt to exercise them. If you can't get at them though, it would suggest to me that they are layered too deep (e.g. you have lots of if/else conditions that need to be satisfied to get to them). That normally suggests, to me at least, that there is a more fundamental problem with the design.

                              Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                              1 Reply Last reply
                              0
                              • H hairy_hats

                                Take the Microsoft option and get your users to test it after release.

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #27

                                Who peed in your oatmeal today?


                                Software Zen: delete this;

                                1 Reply Last reply
                                0
                                • T TheIdleProgrammer

                                  Possibly, but I'm not too interested in the finer details. i just wanted a bit of an informal discussion about other people opinions.

                                  V Offline
                                  V Offline
                                  Vasudevan Deepak Kumar
                                  wrote on last edited by
                                  #28

                                  :) I think as a genuine case, even the Lounge Police seems to have accepted this as an exemption, considering the amount of bona fide responses that the thread has recieved. ;)

                                  Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                                  1 Reply Last reply
                                  0
                                  • T TheIdleProgrammer

                                    Hi everyone, I'm curious about other peoples views on unit testing. The company I work for is quite small and there are only four full time developers. We all have different views on programming styles ranging from very old school, constantly questioning the need for object orientation, through to very modern, arguing for progress and trying to keep everybody up to date. We adopted unit testing around two years ago and, although the overall development process takes a little longer, I think everyone agrees that it's more than proved its worth. In general we have written our unit tests after we have written our code but, for me, it has never really seemed like the correct or logical way to do it so recently I have been trying test-driven development and I have found it to be quite useful. I guess really I am keen to know what other people think about the whole approach to unit testing.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #29

                                    TDD - Test Driven Development. Get it started before the company gets big and you'll have a culture of doing it right. Elaine :rose:

                                    Visit http://www.readytogiveup.com/[^] and do something special today.

                                    1 Reply Last reply
                                    0
                                    • T TheIdleProgrammer

                                      Hi everyone, I'm curious about other peoples views on unit testing. The company I work for is quite small and there are only four full time developers. We all have different views on programming styles ranging from very old school, constantly questioning the need for object orientation, through to very modern, arguing for progress and trying to keep everybody up to date. We adopted unit testing around two years ago and, although the overall development process takes a little longer, I think everyone agrees that it's more than proved its worth. In general we have written our unit tests after we have written our code but, for me, it has never really seemed like the correct or logical way to do it so recently I have been trying test-driven development and I have found it to be quite useful. I guess really I am keen to know what other people think about the whole approach to unit testing.

                                      N Offline
                                      N Offline
                                      Nemanja Trifunovic
                                      wrote on last edited by
                                      #30

                                      Test, schmest. If it compiles, ship it! Or even better, use some interpreted programming language so it doesn't even need to compile.


                                      Programming Blog utf8-cpp

                                      P 1 Reply Last reply
                                      0
                                      • N Nemanja Trifunovic

                                        Test, schmest. If it compiles, ship it! Or even better, use some interpreted programming language so it doesn't even need to compile.


                                        Programming Blog utf8-cpp

                                        P Offline
                                        P Offline
                                        Pete OHanlon
                                        wrote on last edited by
                                        #31

                                        Is this the "Testing is for wimps. We make the big bucks in the support contracts" mindset?

                                        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

                                        1 Reply Last reply
                                        0
                                        • T TheIdleProgrammer

                                          Hi everyone, I'm curious about other peoples views on unit testing. The company I work for is quite small and there are only four full time developers. We all have different views on programming styles ranging from very old school, constantly questioning the need for object orientation, through to very modern, arguing for progress and trying to keep everybody up to date. We adopted unit testing around two years ago and, although the overall development process takes a little longer, I think everyone agrees that it's more than proved its worth. In general we have written our unit tests after we have written our code but, for me, it has never really seemed like the correct or logical way to do it so recently I have been trying test-driven development and I have found it to be quite useful. I guess really I am keen to know what other people think about the whole approach to unit testing.

                                          V Offline
                                          V Offline
                                          Vivi Chellappa
                                          wrote on last edited by
                                          #32

                                          Ship the software and let the users discover the bugs for you. After all, God put users on this planet for a reason.:laugh:

                                          P 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups