DEV Community

Cover image for NUNit and C# - Tutorial to automate your API Tests from scratch

NUNit and C# - Tutorial to automate your API Tests from scratch

Alicia Marianne on September 18, 2023

Did you ever try automate your APIs tests and don't know how to start it? This tutorial will help you to automate your API Test using NUNit and C# ...
Collapse
 
cherryramatis profile image
Cherry Ramatis

Awesome content! I don't know anything about c# but your didactics really helped create a pleasure experience

One quick tip if you allow me:

You can insert the language after the codeblock symbol to enable syntax highlighting like so:

{
  "teste": "teste"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
m4rri4nne profile image
Alicia Marianne

Thanks for the tip, i've updated using it <3

Collapse
 
ipazooki profile image
Mo

Although your approach to testing API was great, I wouldn't use RestSharp and would rather write my own HTTP client. However, your approach was completely robust.

Thanks for your great post.

Collapse
 
m4rri4nne profile image
Alicia Marianne

Thanks! I’ll try do without restsharp next time ❤️

Collapse
 
imadyou profile image
Imad Yousef

Great content! I like it. It's beneficial and easy to understand.

I apologize for being intrusive, but I couldn't help but notice that the FakeRestAPI's response StatusCode are not following the best Rest API practices. The Post method should return 201 (Created), the Delete and Update/PUT methods should return 204 (NoContent)

I appreciate the informative article you've written. Thank you🙂.

Collapse
 
m4rri4nne profile image
Alicia Marianne

Thanks for the information. I didn’t create the API, I just used and create the tests based on swagger documentation. For sure, they not use the best practices for status code 🥲

Collapse
 
fernandoandrade profile image
Fernando Andrade • Edited

Amazing content, I going to use this as reference for when I implementing tests in my projects

Collapse
 
lliw profile image
William Rodrigues

Great post!

Collapse
 
igorsantos13 profile image
Igor Santos

Great article!

Collapse
 
ayodejii profile image
Isaac Ayodeji Ikusika

sweeeeet

Collapse
 
orionth1 profile image
Matheus Emanoel

Nicee

Collapse
 
zoldyzdk profile image
Hewerton Soares

So good content!

Collapse
 
larideoliiveira profile image
Larissa de Oliveira

C# it's my first stack, great content!

Collapse
 
jaywilkinson profile image
James Wilkinson • Edited

Great post, thanks for this!

Just a point though - since your tests are calling a physical web address and checking the response, wouldn't these be integration tests instead of unit tests?

If the website is down, the tests will fail - which wouldn't be a result of anything in the code, but an external system.

Collapse
 
m4rri4nne profile image
Alicia Marianne

I said in the article that is an integration test, NUnit is just the name of the framework.

Collapse
 
jaywilkinson profile image
James Wilkinson

Ah, you did indeed. 😳

Collapse
 
aminmansuri profile image
hidden_dude

I'm skeptical of unit tests that just test for nulls or empty.
Such things can be checked with asserts in the code.

What unit tests should be checking for is that if you write X to the API you get back X. If you ask it to calculate something with the data you gave, it should return the proper calculation.

In other words unit tests should exercise the logic of the API (which you can measure with code coverage tools) and not merely do asserts.

Asserts are better done in the code itself.

I worry many people just write useless unit tests that don't really TEST the application, they just ASSERT certain things about data.

Collapse
 
m4rri4nne profile image
Alicia Marianne • Edited

The purpose of the article is to help those who want to start test APIs using the stack, not to decide or make an analysis about if the tests will be delivering value. As a said in the article, the tests made were integration tests, in a real scenario, probably you’ll have more tests than those, like performance and data consistency, but this is just an example.

Collapse
 
aminmansuri profile image
hidden_dude

Fair enough

Collapse
 
aminmansuri profile image
hidden_dude

Too many simple tests just adds a lot of load to the program while providing little value. Tests need to be sneaky and really push the code to reveal it's bugs.