Automated API Testing Using Powershell


About a year ago I was worked on a project where we had to write a RESTful API from the ground up in Node. After the first few controllers, manual testing become quite tedious and labor intensive. One of the biggest rules in SQA is “automate what you can”. So, once we had a few endpoints solidified to match the RESTful pattern, I got to work. It took about 8 hours to come up with this automated testing solution and it saved countless hours of manual QA and tickets bouncing back-and-forth between QA and DEV (because they didn’t have unit tests). I put the solution on a VM that anyone working on the project could access, modify, and run. Let’s get started with API Testing Using Powershell.

Why Powershell?

I wanted to use the HttpClient from .Net 4.5 that I used in a lot of previous projects. The devs I was working with were MEAN and LAMP stack proficient, so I had to be sure to “Keep It Stupid Simple” and be sure to comment.

The Setup

As previously mentioned, I use the HttpClient (System.Net.Http.dll). I also import the JSON.Net (Newtonsoft.Json.dll) library to parse the JSON response from the RESTful API. In the project you’ll also see a .CSV file (check it out on GitHub). The CSV file is what makes this a data-driven testing solution that is easily updated and read by both machine and man.

The Code

You can view/fork the code from my GitHub project. You’ll need Powershell to run it, but I highly recommend using Powershell ISE. I am not going to copy+paste the code like in my other posts but rather I will explain it at a high-level.

What’s Going On

  1. Get the Present Working Directory (pwd) and save it for the next few steps
  2. Import the .dll files (see “The Setup” above)
  3. Set the BaseUrl, so that only the endpoints need to be specified in the .CSV file
    • This allows you to easily switch environments
  4. Create a new instance of the HttpClient
    • This will hold your session, so that you can log-in with one test and then do some call that requires a valid session afterwards
  5. Import the .CSV file
  6. Parse the .CSV file using a foreach loop
    1. Create an error array
    2. Create a description of the current test (row of the .CSV)
    3. Build a request with the given data
      • If the Method is PUT or POST, then add the body content
    4. Send the request and save the response
    5. Save the response’s status code
    6. Save the response’s content
    7. If the expected status code does not match the response’s, then we add that error message to the error array
    8. If the error array has any length, then we print out an error (with red text-color)
      • Else, print out a success message (with green-text)
    9. If there is a comment in the .CSV file’s current row, then print that out (with gray-text)

API Testing Using Powershell in Action

API Testing Using Powershell Screenshot

Create a BootStrap Enabled MVC5 Web Application

About MVC

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace.

About Bootstrap

Bootstrap was created at Twitter in mid-2010 by @mdo and @fat. Prior to being an open-sourced framework, Bootstrap was known as Twitter Blueprint. A few months into development, Twitter held its first Hack Week and the project exploded as developers of all skill levels jumped in without any external guidance. It served as the style guide for internal tools development at the company for over a year before its public release, and continues to do so today.

What you need to get started

Creating your new project

With MVC4 templates you had to remove/replace the css and html, which was a time consuming practice. The MVC4 template require a lot more work so MVC5 is a boon for developers needing to crank out multiple sites.

  1. Open Visual Studio
  2. Click File, hover over New, and select Project
  3. In the left navigation tree go to click Installed, select Visual C#, then select Web
  4. Name your project and click the OK button
  5. Select MVC as the template and click the OK button
    • You can also select Web API and Unit Test in the add folders and core references section
      • Unit tests are a must for professional/production projects
      • The Web API is a RESTful http service that can be called from other projects like a mobile app
  6. Press [F5] to preview the web application in debug mode
  7. (Optional) Change the web.config file’s database settings
    • By default, the template will create and mount databases as needed.
    • Change the connection string if;
      • You cannot create and mount databases programmatically (an issue I ran into with Arvixe web hosting)
      • You are using database first rather than model first or code first

Further Reading

Getting started with MVC5

  • An in-depth guide from Microsoft

Bootstrap Components

  • An overview of everything included in Bootstrap