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
Β 
zoldyzdk profile image
Hewerton Soares β€’

So good content!

Collapse
Β 
igorsantos13 profile image
Igor Santos β€’

Great article!

Collapse
Β 
larideoliiveira profile image
Larissa de Oliveira β€’

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

Collapse
Β 
orionth1 profile image
Matheus Emanoel β€’

Nicee

Collapse
Β 
lliw profile image
William Rodrigues β€’

Great post!

Collapse
Β 
ayodejii profile image
Isaac Ayodeji Ikusika β€’

sweeeeet

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.