We will be using XUnit for unit testing an asp.net core api
Its a simple employee CRUD api and we will be writing basic unit tests to cover the api code.
It gives better understanding about how to test an API code
Below is the screenshot of all the test execution results

I will quickly go through the test code
EmployeeControllerTest is a test class where all the tests are done here
Here we will be testing Get all employees, Get employee by id, Adding employees, Removing employees.
Also we will be writing tests for incorrect results like get incorrect employee id and more scenarios. i added some comments which will be easy to understand.
InlineData is the one which will be using for passing as parameter to the object. We can have n number of parameters.
Act,Arrange,Assert are the test definitions about how to setup the object
We can write more tests here. Also we can use Mock, Fake and Stubs when testing network, real database. I will write a separate articl about this
A simple question people usually ask is how many tests i need to write and how can i make sure the tests cover the code?
No one can answer how many tests you need to write. It depends on the coverage result. So your code coverage needs to be atleast 80%. Usually more will be better.
Another question people usually ask is how can you know its less than 80% and i can checkin the code and complete my feature work?
After checkin the code , when the builds run at this stage it checks for code coverage. CI/CD can use tools like SonarQube to stop checkin the code before minimum coverage is achieved.
If you use TDD approach your code will be in better testable state.
Code can be found in my Github: https://github.com/pbndru/Phani.Employment