locked
Trying to Understand TDD RRS feed

  • Question

  • I believe I've got a tenuous grip--or at least have found a good starting point for learning--on most TDD concepts, but being new to this party I've got one small piece left that I can't seem to get past.

    How to write tests for code that should run only one time at application startup, such as code that checks for SQLCE database version and performs an upgrade if necessary. This test could take 3-5 minutes every time it runs.

    Ouch!

    Am I looking at this the wrong way?

    Thanks,
    Jeff Bowman
    Fairbanks, Alaska

    Thursday, February 20, 2014 2:21 AM

Answers

  • Hi Jeff,

    Glad to see you again:)

    Maybe I still misunderstood this issue. One result we could make sure it that the class method would be called when you run the test method, so it means that the database have to be updated.

    So the real issue is that we need to change the database back to the previous state after we run the test, am I right?

    I couldn’t make sure what the real app you are developing, but one idea is that you could hand code it, I mean that you could revert it back after the test finished with [TestCleanup] or [ClassCleanup].

    http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

    http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx

    Or you have another class method in your app, and you could create a new test method for it (which could revert back to your database), when you run your test project, you could run two test methods.

    Just a sample: If you have a method to add a data to your database, if the data is wrong or it is not the data you want to add, you would have a method in your app which is used to delete this data, I just give this suggestion from the development perspective even if I’m not the developer. If so, the app would be complete.

    If I have misunderstood anything, please make free feel to let me know.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 2:25 AM
  • Glad to see you again:)

    Likewise :-)

    I couldn’t make sure what the real app you are developing, but one idea is that you could hand code it, I mean that you could revert it back after the test finished with [TestCleanup] or [ClassCleanup].

    Yes.

    http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

    http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx

    Good links, thanks.

    Or you have another class method in your app, and you could create a new test method for it (which could revert back to your database), when you run your test project, you could run two test methods.

    Just a sample: If you have a method to add a data to your database, if the data is wrong or it is not the data you want to add, you would have a method in your app which is used to delete this data, I just give this suggestion from the development perspective even if I’m not the developer. If so, the app would be complete.

    I think you're onto something here.

    I was thinking about it this morning and it all kind of came clear. It seems that I've been looking at it incorrectly; I've been wanting to swallow the whale in one bite.

    Best to cut it up into little pieces instead and chew on it bit by bit. As you pointed out earlier, there's nothing wrong with breaking down the tests into smaller, more manageable units, e.g. CheckDatabaseVersionTest(), UpgradeDatabaseTest(), RevertDatabaseTest(), etc. UpdateMonthlyTotalsTest(), RevertMonthlyTotalsTest(). And so on.

    In fact I believe--correct me, please--that this brings us to the very goal of TDD itself. By writing our tests first, we better shape our code. I've been looking at it the wrong way, from the code-first angle.

    So I think I have a handle on it now. You've been a great help.

    Thanks,
    Jeff Bowman
    Fairbanks, Alaska

    Wednesday, February 26, 2014 3:51 AM

All replies

  • Hi Jeff,

    Would you mind letting us know more detailed information about this issue? Which kind of test are you running? Which kind of app are you testing? Which VS version are you using?

    How to write tests for code that should run only one time at application startup

    If you just create a unit test, for example, you use the VS2012/VS2013, if you run it in test Explorer, the real times are related to your test method, if you use the data driven tests, it would be related to the data source and the running method type like Sequential or Random, if you don’t have the data driven, if just run one time if you run it in Test Explorer.

    If I have misunderstood anything, please make free feel to let me know.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 21, 2014 9:59 AM
  • Would you mind letting us know more detailed information about this issue? Which kind of test are you running? Which kind of app are you testing? Which VS version are you using?

    Hm...  Yes, I guess that would help wouldn't it?  ;-)

    I'm running VS 2013 Ultimate, working on a small WinForms app. I have customers out in the field and I'm preparing a runtime method that will upgrade their SQLCE database on application startup. Naturally it's a one-time-per-customer run--at least until the next upgrade (which may not be coming, but that's another conversation for another time).

    I'd like to be able to test this method, but if it were to run every time I build it'd slow things down unacceptably. I'd also have to work out a way to revert back to a SQLCE 3.5 database after every run.

    Ouch. Surely there must be a 'standard' TDD way to handle something like this? Or am I misunderstanding something somewhere along the line?

    Note: I'm aware that what I'm doing isn't true TDD--this is for an old app that I started years ago. The upcoming rewrite will integrate TDD from the outset. It's going to take some getting used to, but I really like the concept of writing the tests first. I'm looking forward to it and am having a hard time waiting  :-)

    Thanks,
    Jeff Bowman
    Fairbanks, Alaska

    Saturday, February 22, 2014 5:43 AM
  • Hi Jeff,

    Thanks for your response.

    If you mean that you want to test a method which would update the database, maybe you could get better understanding for unit tests here:

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

    I mean that if you class method would update the database, when you create the unit test method, it would call the method of your class when your test running, so it is hard for you to control the updated database in your test method, the real test result is that you need to check whether the database has been updated successfully.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, February 24, 2014 8:27 AM
  • Hi Jack

    Thanks for the link. I'm OK with the nuts-and-bolts of how to actually create a unit test; with this line of questioning I'm trying to understand better architecture.

    My database upgrade scenario is just one instance of the concept. In general, I'm wondering how to best write a test for code that should run only once and not with every build.

    Let's consider for example a method that performs some one-time accounting calculations to produce and store a given result. It's intended to be run only at the end of the month. It runs cumulatively, so that with each run its results are incremented accordingly.

    Other features and functions depend on the values produced by this method, so if we're running it with every build we'll throw the entire application out of balance.

    How best to cover such a method with a test, yet maintain the program's data integrity?

    Thanks,
    Jeff Bowman
    Fairbanks, Alaska

    Monday, February 24, 2014 10:10 PM
  • Hi Jeff,

    Glad to see you again:)

    Maybe I still misunderstood this issue. One result we could make sure it that the class method would be called when you run the test method, so it means that the database have to be updated.

    So the real issue is that we need to change the database back to the previous state after we run the test, am I right?

    I couldn’t make sure what the real app you are developing, but one idea is that you could hand code it, I mean that you could revert it back after the test finished with [TestCleanup] or [ClassCleanup].

    http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

    http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx

    Or you have another class method in your app, and you could create a new test method for it (which could revert back to your database), when you run your test project, you could run two test methods.

    Just a sample: If you have a method to add a data to your database, if the data is wrong or it is not the data you want to add, you would have a method in your app which is used to delete this data, I just give this suggestion from the development perspective even if I’m not the developer. If so, the app would be complete.

    If I have misunderstood anything, please make free feel to let me know.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 2:25 AM
  • Glad to see you again:)

    Likewise :-)

    I couldn’t make sure what the real app you are developing, but one idea is that you could hand code it, I mean that you could revert it back after the test finished with [TestCleanup] or [ClassCleanup].

    Yes.

    http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

    http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx

    Good links, thanks.

    Or you have another class method in your app, and you could create a new test method for it (which could revert back to your database), when you run your test project, you could run two test methods.

    Just a sample: If you have a method to add a data to your database, if the data is wrong or it is not the data you want to add, you would have a method in your app which is used to delete this data, I just give this suggestion from the development perspective even if I’m not the developer. If so, the app would be complete.

    I think you're onto something here.

    I was thinking about it this morning and it all kind of came clear. It seems that I've been looking at it incorrectly; I've been wanting to swallow the whale in one bite.

    Best to cut it up into little pieces instead and chew on it bit by bit. As you pointed out earlier, there's nothing wrong with breaking down the tests into smaller, more manageable units, e.g. CheckDatabaseVersionTest(), UpgradeDatabaseTest(), RevertDatabaseTest(), etc. UpdateMonthlyTotalsTest(), RevertMonthlyTotalsTest(). And so on.

    In fact I believe--correct me, please--that this brings us to the very goal of TDD itself. By writing our tests first, we better shape our code. I've been looking at it the wrong way, from the code-first angle.

    So I think I have a handle on it now. You've been a great help.

    Thanks,
    Jeff Bowman
    Fairbanks, Alaska

    Wednesday, February 26, 2014 3:51 AM
  • Hi Jeff,

    Thanks for your response.

    If some replies are helpful for you, you could mark them as the answer, so I will close this case. If still no help or you need further assistance, please feel free to let me know.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 5:34 AM