Reputation: 35107
I'm fairly unfamiliar with unit testing so please let me know if I'm going about this the wrong way.
Essentially I have a bunch of tests I'm running on a related group of methods. I'd like to check if the getAll()
save()
and get(Id)
methods are working properly. The problem is that if the save()
function works I'm not really sure how to get the resulting element Id into the test that runs against the get(Id)
method.
Am I on the right track or am I violation some rule about how unit tests are supposed to work? What mechanisms are available for me to do this using Visual Studio unit tests?
Upvotes: 0
Views: 104
Reputation: 4060
There are two ways to proceed. First, you could write 3 independent tests to test each function in isolation. The advantage to this method of testing is better granularity when something goes wrong. For example, using the 3-test method, you could get results which show that 2/3 methods (such as save and getAll) are working and the other method is not working.
The other way to proceed would be to write a single test which does (and tests) all three methods in one test. This approach would only tell you the first thing that failed, which might be enough information for you.
It is worth noting that this scenario implies touching a database during the test run, which I would highly discourage. If this is the case, I would instead write tests in the form of 'when I get(), then the following database access code (such as T-SQL) is generated...' and not actually run the database query against the database.
[TestMethod]
public void TestGetAll( )
{
Assert.areEqual("SELECT * FROM People", People.CreateSQLForGetAll());
}
More on this unit-testing technique here.
Upvotes: 1