Unit Testing - NUnit, assertions, exception handling

 Why unit test is useful?

Example if we have 100 tests, and you just want to run test number 25 and 40. This is where you can make use of unit test.


What is a unit test?

Unit test is essentially a framework.

A framework is a pre-defined structure that provides a starting point for building software. It includes reusable code modules, libraries, compilers, and other components

There are many frameworks to do unit testing such as Nunit, Xunit etc.

What is a design pattern?

Design pattern is how you structure your tests. How your code is presented in your solution.

Example, you have test class, and then pages and codes are inside the pages etc. 


NUnit Annotations:

- SetUp

- Test

- TearDown

- TestFixture

Not commonly used:

- OneTimeSetUp

- OneTimeTearDown


NuGet Packages

Microsoft.NETCore.App is the package that allows to run tests as a dot net core application. This is essentially a framework.

For NUnit testing, to install below NuGet packages:




NUnit package allows us to use the annotations as above

NUnit3 test adapter allows us to visually see the tests separately

-----

Assertions

To test, we can use assertions


The one underlined in red is preferred.

--------

Exception Handling

Exceptions handling example:



Comments

Popular posts from this blog

Specflow, Gherkin

DevOps, Github

Test Automation tool, VS, selenium, Github, Gitbash