C# | Rest API Testing: How to create an API Testing Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the qa underground in today's video we're going to be talking about how to create an api testing framework using c-sharp and visual studio if you're new to the channel but enjoy testing related content feel free to hit the subscribe button down below or hit the like button on the way out we definitely appreciate the support with the intro out of the way let's get into what today's video will cover in today's video we're going to be talking about how to create an api testing framework we'll achieve this by performing the following steps first we'll create a new test project then we'll create a new services project followed by creating a services builder class then we will add a reference to the services project to the test project finally we will create a test method that tests the api using references from the services project and run the test and verify the first thing we're going to do is create a new project so we'll click new we're going to go under web and console we'll select tests and we'll select end unit test project and then next click next for project name we're going to enter qa underground api test and then we'll click create once that's open we can delete the automatically generated unit test right click delete confirm with delete so you'll notice here we only have one project right so that's what the project that we created initially which is qa underground api tests ideally when i'm working with api tests i like to create two separate projects one being the services project and another being the test project that calls the services project and why i do that is because i like to have the services be its own project and house where we will create our uris for multiple different apis right so most projects will contain more than one api so we want to make sure that we have a project that's dedicated to building the uris that we need to test with and then we can call them from the test project so we'll start by creating our new project we'll go up to our solution right click add click new project we're gonna under the web and console we'll select tests select next make sure.net cored 3.1 select next and then for the project name we're going to call this services then we'll click create again we can delete the unit test that's automatically generated and then within the services project we're going to right click we're going to add a new folder we're going to call that folder weather click add and then we're going to right click on weather and click add then new file then we're going to add an empty class called weather api and then select new so in here we're going to get rid of this generated code now we're going to set up our http client so we're going to do private http client we're going to call the variable rest client new http client we'll close it off parentheses so now we're going to add the dependency for that so we'll do quick actions we're going to use using system.net.http [Music] next we're going to do private string uri equals and then we're going to pass in the endpoint for our api so for this case i'm using the weather.gov endpoint now we're going to do a public async call so because we're doing a call to the end point so we'll do public async task we're going to pass in string because that's the type we want returned we'll do git underscore we're actually going to want the reason phase returned here for our test example so we'll say git underscore reason phrase and i'll make a little comment here so this is where we're gonna build the uri we're first gonna start with our request headers and these headers i got from sending this request in postman or you can find them other means we're gonna start with rest client dot default request headers dot try add without validation and inside there we're gonna pass in the headers that we want to be passed along when we send our request so i'm actually gonna fill these in with the request headers that i got from postman [Music] and in postband i know that there are three of them so i'm gonna actually make a generic one here and i'm gonna copy it two additional times [Music] and then again i'm gonna fill in the request headers that i got when i ran this call through postman so [Music] me [Music] oh once that's done we can add our dependency for task which is system threading dot task [Music] next we'll make a note here that says makes the get call to the rest endpoint we'll start with doing a var response equals await rest client dot get async and then we're gonna pass in our uri [Music] we'll make another note here that says read the reason phrase from the response as a string so now we're going to do a var reason phrase equals response dot reason phrase dot to string so we can read it and then finally we're simply going to return our reason phrase back to the test and we can get rid of this unused dependency at the top here and finally we will file save so now we want to add that project to our test project so we're going to right click add reference and inside here we're going to select the projects tab and select our services project and select okay [Music] so we'll clean and rebuild [Music] so if we look inside of our dependencies we'll see a projects folder now dependency and then we'll see our dependency on the services that means that our test project can now access the services so we're going to right click add new folder we're going to call this folder weather api we'll click add we'll right click on weather api click add click new file add a new empty class called weather api underscore tests and we'll click new so we can get rid of this using statement at the top we won't be using that i'm also getting this generated public method inside of the class so we'll start by creating some attributes we'll add our test attribute we'll add the dependency for that which is using end unit dot framework and then below that we're going to add a category attribute and we will call that api tests this is a simple way to keep your tests organized when you're running them we'll start off by doing a public async task we'll call it api underscore tests [Music] we'll add the reference for task which is again system.threading.tasks inside there we'll create our instance of weather api so we'll do weather api call our variable weather api equals new weather api [Music] try adding the reference for weather api up here so we'll do services.weather still is airing so we'll clean our build build it see if that fixes our air and it did not so we'll just directly reference weather.api so we'll do services.weather.weather api and then we'll do the same on the other side to fix the error i'm not sure why that didn't resolve itself when i added the reference above if you know please leave a comment down below next we'll do our var we'll do reason phase because that's what we want to come back equals await and then we'll do we'll call that method within the weather api so we'll do weather api dot get underscore reason phrase and then we'll add an assertion just to validate that it is returning what we want so we'll do assert.rt and then i'm expecting the reason phrase to say okay so i'll pass okay as my expected and then we'll pass and the reason phrase so before we run it we will file save we'll do a clean we'll build all verify we have no errors it doesn't look like any so i'm going to go down to the test tab and inside of our test project we should have a test now and we will right click run test [Music] and it looks like it passed so normally if you were running things and you wanted to see the output you could do show results and the output from any like console write lines would show up in output in today's video we covered how to create an api testing framework using the c sharp and visual studio we went over how to create a test project create a services project and then add a reference to that services project to that test project and create a test method that utilized the services project to test the api once again if you're regular or new to the channel let me know if you like this style of content by hitting the like button down below and if you're not already a subscriber to our channel but enjoy the content i'd encourage you to hit the subscribe button on the way out thanks for tuning in and i will see you on the next video [Music] oh
Info
Channel: QA Underground
Views: 14,289
Rating: undefined out of 5
Keywords: Tutorial, API Testing, Visual Studio, Automation, Automation Testing, VS, API, Testing, Learning, Tester, fast, easy, rest, rest api, automate, tfs, team foundation server, test, jmeter, load testing, performance, performance testing, load generator, C#, restful, code, coding, lesson, step by step, windows, rest api tutorial, rest api tutorial c#, visual studio api, visual studio api testing, automated api testing using c#, how to automate rest api testing with visual studio, CSharp API Testing
Id: pQ3Wv41cbYg
Channel Id: undefined
Length: 13min 16sec (796 seconds)
Published: Wed Feb 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.