c# - Integration test good practice -
i using unit test time. need write integration test. should save db, , check if saved data ok. can't find simple clean example of integration test. idea this:
[test] public void integrationtestexample() { // arrange without mocking var objec1 = new objec1(); var objec2 = new objec2(); var starttestclass = new starttestclass(objec1, objec2); var savedata = "test data"; //act starttestclass.savetodb(savedata); // assert var valuefromdb = selectsaveddata(); assert.areequal(savedata, valuefromdb); } //get data db assert private string selectsaveddata() { var sqlquery = "select top 1 data table1"; var data = repositoryfortest.selectsaveddata(sqlquery); return data; }
but not sure if approach? have suggestions?
you should test 1 piece of integration @ time, , leave db in such state can run multiple tests , should not affect each other. ms test framework allows define methods setup , cleanup tests @ times during test run, shown in following code
[testclass] public class returnservicetest { [classinitialize()] public static void classinit(testcontext context) { //init test data class } [testcleanup] public void testcleanup() { //cleanup data after each test } [testmethod] public void integrationtestexample() { // arrange without mocking var objec1 = new objec1(); var objec2 = new objec2(); var starttestclass = new starttestclass(objec1, objec2); var savedata = "test data"; //act starttestclass.savetodb(savedata); // assert var valuefromdb = selectsaveddata(); assert.areequal(savedata, valuefromdb); } //get data db assert private string selectsaveddata() { var sqlquery = "select top 1 data table1"; var data = repositoryfortest.selectsaveddata(sqlquery); return data; } }
Comments
Post a Comment