1 person found this helpful
The problem is that the Test case (DBTest) itself is recreated every time. Ideally, tests are supposed to run in isolation and shouldn't have anything to do with each other... state between tests makes them order dependent, etc...
So, if you really want to do this, you would need to store your variable in a static property or in some other class like a singleton.
Other options, you could organize things a bit differently and use a static method marked BeforeClass method, which will only run once before the class is instantiated for the first time... then all of the other tests would run... then an AfterClass method could be used for cleanup.
Thanks for the quick answer. Yes, I figured it was recreating itself for each test. I definitely like the approach of putting things in the BeforeClass and AfterClass approach as this seems to fit the isolation paradigm more strictly and would like to reorganize things to fit this.
My only issue/question is that I would like this to be a test. As far as I can tell, things in [Before] and [BeforeClass] cannot be tests themselves. Is this correct?
Correct. The downside of trying to test this way is that you have things that are partially setup and partially test.....
You aren't going to find an easy answer here. Best I can offer is that you have some duplication. You have the create table as a test somewhere in a test case and use it as a test. Using order or another mechanism you can make sure it runs first as a smoke test. In that same test case (as cleanup in After) destroy the table.
Then for your other tests make it part of setup and not a test.
When you have things that are sometimes setup and sometimes tests it is an indication that you are testing a lot of pieces at once. I usually recommend people avoid that, but given what you are trying to accomplish is test a manager, database and methods all at once, I am not sure there is going to be a better way without breaking it down differently.
If you really want it all just to be a test case, then storing the needed info in a static or something else is just as viable. Neither of these approaches is isolated so I wouldn't let that be the decision making factor.
Thank you very much for the explanation and quick help.