locked
Unit Testing Newbie - Unit vs Integration RRS feed

  • Question

  • Hi all,

    I recently read Roy Osherove's book, The Art of Unit Testing, and am in the process of creating unit tests for my application using his recommendations. I decided to start with something simple. Below is a description of what the final environment will look like.

    I will have a User object that has a ChangePassword method. The method itself will return excetions if certain conditions are met. For example, if the old password and new password are the same, an exceoption will be thrown. The messages for the exceptions will be stored in the database so that we can reuse the messages across our applications. The application will retrieve the messages from the db and cache them for future use.

    When creating the unit test cases, I started by simply hardcoding the expected exceptions in the test case and in the ChangePassword method that was being tested. I went through the Fail -> Pass process and am ready to refactor my ChangePassword to use the environment above. My question is about the test itself. If I refactor the ChangePassword as described above, won't my unit test become an integration test? It seems like I will have to set configuration settings in the test project and based on Osherove's recommendations, won't that be a big No-No?

    Any help would be greatly appreciated.

    J. Espana

    Friday, February 18, 2011 2:23 PM

All replies

  • hi,

    For example, if the old password and new password are the same, an exceoption will be thrown.

    This is not a good design idea, see

    http://blogs.msdn.com/b/chris.green/archive/2008/02/11/don-t-use-exceptions-to-control-application-flow.aspx


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Friday, February 18, 2011 2:31 PM
  • Hi Stefan,

    Thank you for the suggestion, but the question still remains. Even if the error is being returned, I still have the same question regading the test.

    Thanks.

    Friday, February 18, 2011 2:38 PM
  • hi,

    it should have answered your question. Instead of returning an variant text message you should return an invariant (numeric) constant. The displayed error message depends on the constant and is not part of your unit test.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Friday, February 18, 2011 3:36 PM
  • Hi Stefan,

    Thanks again for the reply. I understand what you are saying about my question above. And looking at your responses again, I definitely agree with what you say. By returning a value, I can just test against the constant. Got it.

    But, eventually, I will need to verify that the logic, that retrieves the error messages from the database, is working correctly. At that point, will I be programming a unit test or and integration test? I would like to test that my object is being loaded correctly, that it is being cached, and that if I ask for a given message id, that I get the correct message back from the cache. This sounds like an integration test to me.

    Thanks again.

     

    Friday, February 18, 2011 6:50 PM
  • Hi,

    This is a great article which can help you http://netindonesia.net/blogs/jimmy/archive/2010/07/07/unit-testing-mocking-stubbing-and-moles.aspx

    You also try out pex and moles, refer http://research.microsoft.com/en-us/projects/pex/

    Thanks,

    Anuj

    Friday, February 18, 2011 10:37 PM
  • hi,

    the retrieval of the error messages is a different component, thus you still run a unit test here to verify, that it works.

    A integration test would be to verify that the entire output of User class maps to message component, but this can be still done as a unit test. Use a data driven unit test which feeds the message component with all defined outputs.

    http://msdn.microsoft.com/en-us/library/ms182527.aspx


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Saturday, February 19, 2011 7:16 PM