How do I check “no exception occurred” in my MSTest unit test?

后端 未结 6 2008
再見小時候
再見小時候 2020-12-30 18:34

I\'m writing a unit test for this one method which returns \"void\". I would like to have one case that the test passes when there is no exception thrown. How do I write t

6条回答
  •  心在旅途
    2020-12-30 18:52

    My friend Tim told me about ExpectedException. I really like this b/c it is more succinct, less code, and very explicit that you are testing for an exception.

    [TestMethod()]
    [ExpectedException(typeof(System.Exception))]
    public void DivideTest()
    {
        int numerator = 4;
        int denominator = 0;
        int actual = numerator / denominator;
    }
    

    You can read way more about it here: ExpectedException Attribute Usage.

提交回复
热议问题