public void TestMyFunction() { string input = null; // Test if an exception of type MyCustomException is thrown Assert.Throws(() => MyFunction(input)); } public void MyFunction(string input) { if (input == null) { throw new MyCustomException("Input cannot be null!"); } }
public void TestDoSomething() { // Test if a DivideByZeroException is thrown Assert.ThrowsThis code examples use the NUnit package library, which provides a set of extensions and constraints for writing unit tests in C# using the NUnit framework. The Throws TypeOf keyword is part of the NUnit framework.(() => DoSomething()); } public void DoSomething() { int a = 5; int b = 0; int result = a / b; // throws DivideByZeroException }