c# - How to execute code as steps using a framework or built in functionality? -
i'm looking framework or built in way write elegant code easy maintain following.
this example:
in main method want 3 things:
- buy cake
- put icing on cake
- serve cake
only once 3 actions complete consider operation success. if 1 particular action fails, want know failed.
to solve problem main method can end looking this:
public void isbirthdayasuccess() { var birthdaymanager = new birthdaymanager(); try { var cake = birthdaymanager.buycake(price.cheap, quality.good, delivery.fast); assert.istrue(cake.arrivedquick && cake.isgood && cake.isquick, "supplier didn't cake right"); } catch { assert.fail("the birthday operation has failed on step 1 : buying cake failed"); } try { birthdaymanager.cakes[0].puticingon("vanilla"); assert.istrue(birthdaymanager.cakes[0].isiced(), "icing cake didn't work"); } catch { assert.fail("the birthday operation has failed on step 2 : putting icing on cake"); } try { party.servecake(birthdaymanager.cakes[0]); assert.istrue(birthdaymanager.cakes[0].isserved(), "cake couldn't served"); } catch { assert.fail("the birthday operation has failed on step 3 : serving cake"); } }
now in reality, "steps" here represented series of try/catch blocks in code, elsewhere across enterprise system. why looking framework can provide more consistent implementation of "steps" framework inside function.
i thinking of rolling own maybe else exists.
if rolled own end being this:
public void testmethod(int id) { var test = new test(id); teststeps { ["login application"] teststep { test.logintoapplication(); } assert, "you not logged in" { test.isloggedin(); } failure { assert.fail("test failed on step " + testcontext.teststep.index); } }
does framework exist?
edit: reuse, able iterate through "steps" collection , "step" items, meaning can use reporting in excel or other places.
edit 2: , "roll own" example designed replace semantics of try/catch , flow control, c# extendible?
take @ specflow: http://www.specflow.org/, great step-based test framework.
Comments
Post a Comment