简体   繁体   中英

nUnit Assert.Throws TestCase attribute

I have a test method like this:

[TestCase(16486, "BobBank2.site16486.1", "16486.1")]
[TestCase(16441, "BobBank2.site16441.1", "16441.1")]
[TestCase(16443, "BobBank2.site16443.1", "16443.1")]
public async Task CheckUserAccountLinkStatusTest(int providerId, string username, string userKey)

I need to make sure that the first case (16486) throws an error, always. Is it possible to modify the TestCase attribute to do this?

You could check

if(providerId==16486)
{
    Assert.Throws(...
}

but it would be better to have two separate tests, one that checks for the exception when it is expected and one that asserts that there is no exception when there shouldn't be.

While the @Connel.O'Donnell solution works, I'd suggest to:

  1. Avoid conditional logic in unit tests.
  2. Separate tests that throws exception from those which doesn't throw one.

So, your new, separated test for asserting exception should more or less look like:

[TestCase(16486, "BobBank2.site16486.1", "16486.1")]
public async Task CheckUserAccountLinkStatusTest_ThrowsException(int providerId, string username, string userKey)
{
    // ...
    Assert.That(() => { /* ... */  }, Throws.Exception);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM