Email Subscription Form

Saturday, February 2, 2019

Easy Free Automation Part III: Services Tests

This week we are looking at my favorite type of automated tests: services tests.  The reason I love services tests is because they test so much of the application without the hassle of the UI.  There are many types of services, but the most widely used service is the REST API.  And my favorite way to test a REST API is with Postman


I'm not going to get into too many details about REST verbs or about Postman, because I have already done so in earlier blog posts.  If you'd like to catch up on what I've written, you can start with this post and work your way forward week by week.  You can also read this tutorial that I wrote for Simple Programmer.  

This post will focus on how you can automate Services Tests.  To demonstrate, I've created a collection of Postman tests that you can download from Github here.  The API I'm testing is called Restful-Booker: it's a great easy API created by Mark Winteringham to help people learn how to test APIs.  

Once you have downloaded the two files from Github, you can upload them in Postman to see what they do.  To upload the collection, click on the Import button on the top left of the Postman window:


When the Import window pops up, click the Choose Files button and then navigate to the location of the file you downloaded called restfulbooker.collection.json.  Click on that file name, and the collection will be imported:  


Next, you'll need to upload the environment file.  The environment file is what contains the variables that are used in the requests and assertions.  To upload the environment file, click on the gear icon in the top right of the Postman window:


When the environment window pops up, click on the Import button, then on the Choose Files button, then navigate to the location of the downloaded file called restfulbooker.env.json.  Click on that file name and the environment will be imported: 


Finally, in the dropdown menu in the top right, select the Restful Booker environment: 


You are now ready to run the requests from within Postman.  Keep in mind that the tests are not idempotent: some of the requests depend on previous requests for setting variables and creating bookings, so while you are exploring you should run the requests in order.

Let's take a look at one of the requests and its assertions:


This is a GET request.  The URL of the request is https://restful-booker.herokuapp.com/booking/1.  The request is asking for the booking with the id of 1.  You can see two tests in the Tests section: the first test verifies that the response code of the request is 200, and the second test verifies that "firstname" is one of the fields in the response.  

Click the Send button to run the request, then scroll down to the bottom of the page:


Here you see the body of the response, which has returned a booking.  You can also see a tab called Test Results, which shows that 2 out of 2 tests pass.  If you click on this tab, you can see your results:


Now that you understand how the tests and assertions work, let's run them from the command line!  In order to do this, you will need to have node.js installed.  Installing node will also install npm, which is the node package manager.  Then you can use npm to install Newman, the package that is used to run Postman tests.  To install Newman, simply open your command-line window and type: 

npm install -g newman

Once Newman is installed, navigate in the command-line window to the place where your restfulbooker.collection.json and restfulbooker.environment.json files are located.  Now you can run your tests with this command::

newman run restfulbooker.collection.json -e restfulbooker.environment.json

(This command should be in a line with no returns; it's OK if it wraps around to a second line, though.)

The "-e" in this command stands for "environment".  If all goes well, you should get a result that looks like this:  


You can also run your tests from other file locations.  To do this, simply specify the path to your files in your command.  For example, if my files were in a folder called NightlyTests, which was in my Documents folder, and if I were using a Windows computer, I would use this command:

newman run C:\Users\KJackvony\Documents\NightlyTests\restfulbooker.collection.json -e C:\Users\KJackvony\Documents\NightlyTests\restfulbooker.environment.json

Now that you know how to run your tests from the command line, you can set up your tests to run automatically with a cron job or in a CI/CD platform such as Jenkins.  Just be aware that whatever machine you run your tests on will need to have Newman installed.  

You can also have your test results write to a file with the -r command.  For example, you can have your results written in junit with this command:

newman run restfulbooker.collection.json -e restfulbooker.environment.json -r junit

When the tests are finished running, a Newman folder will be created with your test results inside.

I hope that this tutorial has given you some ideas about how you can automate your API testing!  Next week we'll move on to UI tests with Selenium.  




54 comments:

  1. Great post Kristin. Would love to see a follow up with your thoughts on what to test in services, such as contract validation etc. Also since REST APIs are stateless if you've encountered good ways to get around (hooks) initial authentication and basic calls to directly get to the endpoints being tested. Thanks!

    ReplyDelete
    Replies
    1. I'm glad you enjoyed my post! I talked a bit more about what to test in API services in my blog posts from last February and March, so be sure to check those out! As for authentication, I've never found it to be a hassle in API testing, because it's so easy to save the token or cookie as a variable and use it in all subsequent requests. But another option would be to use mocks or stubs. Postman has mocking capabilities, but I haven't used them, so can't comment on how they work. However, I have written stubs directly into the code that will return a positive response for any call to the authenticated service, so that might be a route you could take. I hope this helps!

      Delete
  2. The article is more informative. This is more helpful for our software testing training institute in chennai. Thanks for sharing

    ReplyDelete
  3. Top quality blog with excellent writing skills and information was very valuable thank you.
    Data Science Course in Hyderabad

    ReplyDelete
  4. Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
    Cyber Security Training in Bangalore

    ReplyDelete
  5. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter. Here is deep description about the article matter which helped me more.
    Data Science Course

    ReplyDelete
  6. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
    Data Science Institute in Bangalore

    ReplyDelete
  7. Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.
    Data Science Training in Pune

    ReplyDelete
  8. I think it would be easier to make video how you do it and publish on youtube. From this site https://viplikes.net I usually buy youtube likes for my video. You can do it too

    ReplyDelete
  9. Excellent Blog! I would like to thank for the efforts you have made in writing this post. Content shared was valuable and informative.
    Data Science Courses in Bangalore

    ReplyDelete
  10. have expressed a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
    Digital Marketing Training Institutes in Hyderabad

    ReplyDelete

  11. I see some amazingly important and kept up to a length of your strength searching for in your on the site

    Digital Marketing Training Institutes in Hyderabad

    ReplyDelete
  12. Thanks for the Valuable information.Really useful information. Thank you so much for sharing. It will help everyone.

    SASVBA is one of the leading training providers in the country. We provide AI training Course in Delhi accordance with current industry standards, allowing students to find their dream job in the world's leading companies. SASVBA specializes in teaching services, artificial lighting training that imparts practical knowledge through live project-based learning.
    FOR MORE INFO:

    ReplyDelete
  13. Have you ever think to create tiktok profile? You could post there your test video. Read from here https://n4gm.com/tiktok-promotion-the-main-whys-and-hows-explained-to-you-by-professionals/ how to get followers for tiktok and think about it one more time

    ReplyDelete
  14. It's like you understand the topic well, but forgot to include your readers. Maybe you should think about it from several angles.

    Business Analytics Course

    ReplyDelete
  15. Thanks for your efforts in sharing this post with us. This was really awesome. kindly keep continuing the great work.
    Data Science Training in Hyderabad
    Data Science Course in Hyderabad

    ReplyDelete
  16. Thank you for this post! I would make a video and publish it o youtube on your place. I have youtube channel where I always post my video. I buy youtube subscribers from here https://soclikes.com/ to grow my channel

    ReplyDelete
  17. Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.
    data science course in malaysia

    ReplyDelete
  18. Thank you so much for doing the impressive job here, everyone will surely like your post.
    data science course in malaysia

    ReplyDelete
  19. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    cyber security course in nashik

    ReplyDelete
  20. A great website with interesting and unique material what else would you need.
    data scientist course

    ReplyDelete
  21. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.
    data science course

    ReplyDelete
  22. Thanks for posting this info. I just want to let you know that I just checked out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
    ai course in hyderabad

    ReplyDelete
  23. Thank you for sharing this wonderful blog, I read that Post and got it fine and informative. Please share more like that...
    Ethical Hacking Institute in Bangalore

    ReplyDelete
  24. I wanted to thank you for this great read!! I definitely enjoying every little bit of it waiting for next one.
    Data Analytics Course in Chennai

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Really impressed! Information shared was very helpful Your website is very valuable. Thanks for sharing.
    Data Science Course in Bangalore

    ReplyDelete
  27. Wonderful content! Keep working on it! And if you have time, try checking out our website

    Web Design Company in Norwich

    Web Development Company in Norwich

    ReplyDelete
  28. I found this blog very informative for automation services.
    Do not miss out on the tremendous potential that AI and machine learning brings into the table. Contact us today to make the best of the AI based automation technologies services that we have.
    Milestone Technologies provides global IT managed services that helps companies get the most of the digital and technology solutions such as Automated IT Service Desk, Digital Workplace Services, Modern IT asset lifecycle management, Modern AV VC Digital solutions, data center infrastructure services, Cognitive Automation solutions, Workforce Solutions

    ReplyDelete
  29. Private Toto is an online company that provides Internet sports betting. Various betting items will be provided to Toto users and the winner will be refunded according to the set dividend rate. And among Toto sites, private Toto sites with excellent capital are called safety playgrounds. 토토사이트 메이저놀이터 안전놀이터

    ReplyDelete

  30. This is a great inspiring blog.You have shared really very helpful information thank you.
    Data Scientist Course in Amritsar

    ReplyDelete
  31. Do not miss out on the tremendous potential that AI and machine learning brings into the table. Contact us today to make the best of Cognitive Automation solutions technologies that we have.

    ReplyDelete
  32. This is an excellent article. I like this topic. I have found a lot of interesting things on this site. Thanks for posting this again.
    Business Analytics Course in Jaipur

    ReplyDelete
  33. This post is very simple to read and appreciate without leaving any details out. Great work!
    data science course in hyderabad

    ReplyDelete
  34. We provide well-informed, single points of contact for users to resolve issues and submit service requests. Contact us to know more about IT service desk automation

    ReplyDelete
  35. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
    cyber security training malaysia

    ReplyDelete
  36. 토토365프로 Hi to all, the contents present at this site are actually amazing for people experience, well, keep up the nice work fellows.Feel free to visit my blog post..

    ReplyDelete
  37. There are a very interesting question and very positive thinking 배트맨토토

    ReplyDelete
  38. I absolutely love your blog and find the majority of your post's to be what precisely I'm looking for. 스포츠토토티비

    ReplyDelete
  39. Superb Information, I really appreciated with it, This is fine to read and valuable pro potential, I really bookmark it, pro broaden read.
    Appreciation pro sharing. I like it.

    ReplyDelete
  40. K-Nearest Neighbour (KNN) algorithm is one of the uncomplicated ML algorithms based on the Supervised Learning Technique. It assumes the similarities between the new and available data and categorizes them based on pre-existing groups while storing all the available data. KNN incorporates a data point based on similarities of new and old data. It is used for both regression and classification of data, but the latter is used frequently. To learn more about KNN start your Data Science training today with 360DigiTMG.

    Data Science in Bangalore

    ReplyDelete
  41. It would help if you thought that the data scientists are the highest-paid employees in a company.
    data science course in kochi

    ReplyDelete
  42. I’m looking forward to reading more informative articles like this. It contains a wide spectrum of details and information for students who want to enrol in a proper course curriculum and are looking for good options. Best Data Science Institute In Chennai With Placement

    ReplyDelete
  43. Thank you for sharing the valuable article with us. if you want to know more about Plant automation In Cement Industry click here and read the article

    ReplyDelete
  44. Discover viAct cutting-edge EHS Monitoring Solution
    and revolutionize your workplace safety. Monitor, analyze and improve with ease. Request a Demo.

    ReplyDelete
  45. The success of a test automation company largely depends on the tools and technologies it employs. At Kualitatem, we utilize a wide array of advanced automation tools and frameworks to deliver exceptional test automation services.

    ReplyDelete

New Blog Location!

I've moved!  I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted.  So I&#...