开发者

Unit Testing (C#)

I am unit testing some CRUD operations. My questions are:

1) If I need to test the Add, Get开发者_运维问答, and Delete methods. The persistence layer is a database. Since I need to have a test object to Get and Delete, should I combine all 3 of these into one [TestMethod], or separate these into 3 methods and re-add the object before the Get and Delete tests?


Ideally you should have individual tests for each case.

You should use some sort of mocking - either via a framework or by setting up the database yourself - to set the initial conditions for each test.

So to test add you would start with a blank database and then add some new data, try to add the same data again (it should fail), add incomplete data etc.

Then to test get and delete you would start with a pre-populated database and perform the various tests you need.


I'd generally make a separate test. If I'm testing a "get" type method, the test setup would insert the object (generally by way of some mock framework) I expect to get as necessary, it just wouldn't be asserted against in the same way the actual get would.

This does mean that if the add implementation breaks, both the tests for the get and the add will fail in one way or another, assuming proper coverage. But that's kind of what you want anyhow, right?


If you are writing you own ORM to handle CRUD, I suggest you separate each action in a different test. Don't create big tests, that has many points of failure and many reasons to change, because it will turn your test project hard to maintain. Test each feature separately.

Now, if you are using some third part ORM to deal with CRUD, you should not test the tool at all, unless you don't trust it. But, in this case, you should find a better alternative. :)

You can do some Acceptance Tests to check if everything is working and, at this time, you will really reach the database.


Whatever makes testing easier for you :) as long as you get a return stating which method passed/failed then it should be good.


Hello :) I'd separate and test individually, and set up cleanly before and clear up after each.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜