c# - How to change the Assert.Fail message -


when run test "assert.fail failed. assert.areequal failed. expected:. actual:."

how test either pass or fail without "actual:" @ end of error message? use "assert.fail(ex.message);" in several other test cant change message directly.

    [testmethod]     public void testcreateuser()     {        try        {           asamembershipprovider prov = this.getmembershipprovider();           //call user           membershipcreatestatus status;           membershipuser user = prov.createuser("testuserx", "12345", "test.userx@abc.com", "", "", true, null, out status);            assert.arenotequal(status, membershipcreatestatus.success);           var isauthenticated = prov.validateuser(user.username, "12345");            assert.istrue(isauthenticated);           assert.areequal(user.username, "testuserx");           assert.areequal(user.email, "test.userx@abc.com");           assert.istrue(user.creationdate==datetime.now);           //todo asserts         }         catch (exception ex)        {           logmessage(ex);           assert.fail(ex.message);        }     } 

additional info added: unit tests (in general) should check single component. test remove call validate user should it's own check in it's own test method.

this implies 2 test methods createuser_issuccessful_ifcreatinguserthatdoesnotexist() validateuser_authenticates_ifgivencorrectusernameandpassword()

which more descriptive testcreateuser method name , allows do finer grained testing. next test createuser_fails_ifrecreatingexistinguser().

it's difficult give advice not know requirements project working on. if must have customized output have suggested work (but it's not best practice , feels bit hack me). better solution more this:

[testmethod] public void testcreateuser() {     asamembershipprovider prov = this.getmembershipprovider();     //call user     membershipcreatestatus status;     membershipuser user = prov.createuser("testuserx", "12345", "test.userx@abc.com", "", "", true, null, out status);      //assert.arenotequal(status, membershipcreatestatus.success);     if (status != membershipcreatestatus.success)         assert.fail("error message want goes here case.");     var isauthenticated = prov.validateuser(user.username, "12345");     //assert.istrue(isauthenticated);     if (!isauthenticated)         assert.fail("error message want goes here case.");     //assert.areequal(user.username, "testuserx");     if (user.username != "testuserx")         assert.fail("error message want goes here case.");     //assert.areequal(user.email, "test.userx@abc.com");     if (user.email != "test.userx@abc.com")         assert.fail("error message want goes here case.");     //assert.istrue(user.creationdate==datetime.now);     if (user.creationdate != datetime.now)         assert.fail("error message want goes here case."); } 

which customized error message , removed clunky try catch not necessary.

i leaving original output before answer accepted, agree comment try catch shouldn't used in way (hence correction above). time use try catch in test if testing scenario specific type of exception raised raised if business rule violated

try {     methodtothrowexception();     assert.fail("businessspecificexception not thrown code."); } catch (businessspecificexception ex) {     //asserts go here } 

if wanting funnel asserts through catch block , want customize error output achieved this:

    [testmethod]     public void testcreateuser()     {        try        {           asamembershipprovider prov = this.getmembershipprovider();           //call user           membershipcreatestatus status;           membershipuser user = prov.createuser("testuserx", "12345", "test.userx@abc.com", "", "", true, null, out status);            //assert.arenotequal(status, membershipcreatestatus.success);           if (status != membershipcreatestatus.success)               throw new exception("error message want goes here case.");           var isauthenticated = prov.validateuser(user.username, "12345");            //assert.istrue(isauthenticated);           if (!isauthenticated)               throw new exception("error message want goes here case.");           //assert.areequal(user.username, "testuserx");           if (user.username != "testuserx")               throw new exception("error message want goes here case.");           //assert.areequal(user.email, "test.userx@abc.com");           if (user.email != "test.userx@abc.com")               throw new exception("error message want goes here case.");           //assert.istrue(user.creationdate==datetime.now);           if (user.creationdate != datetime.now)               throw new exception("error message want goes here case.");           //todo asserts         } 

and test method still run assert.fail portion. assert methods behind scenes doing similar (though throwing derived exception type instead of base) internally.

as high level advice unit testing providers difficult. i've created custom 1 in past , nightmare rewrite in such way allow me control inputs , outputs. had provide constructor allowed me pass interfaces in external dependencies allow me write tests. when did able write tests such

returnscreateduser_ifcreationissuccessful() or returnsinvalidpassword_ifpasswordisinvalid()

where asserts looked this: assert.areequal(membershipcreatestatus.success, _status); assert.isnotnull(response);,

and

assert.areequal(membershipcreatestatus.invalidpassword, _status);.

that secondary issue run when trying test provider. throwing exceptions message want allow customize message completely.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -