├── README.md └── course-notes.md /README.md: -------------------------------------------------------------------------------- 1 | # 👋 API Test Automation with Postman 2 | 3 | Hello and welcome to this free course on automating API testing with Postman. 4 | 5 | [![Watch the video on freeCodeCamp](https://img.youtube.com/vi/zp5Jh2FIpF0/sddefault.jpg)](https://www.youtube.com/watch?v=zp5Jh2FIpF0) 6 | 7 | ## Important links 8 | 9 | * [📝 Course notes](./course-notes.md) 10 | * [📚 Postman workspace](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api) 11 | 12 | ## Get in touch 13 | 14 | * [📺 Subscribe to my YouTube channel](https://www.youtube.com/@vdespa?sub_confirmation=1) 15 | * [🐦 Twitter](https://twitter.com/vdespa) 16 | * [🏢 LinkedIn](https://www.linkedin.com/in/vdespa/) 17 | * [🎓 My other Courses](https://vdespa.com/courses) 18 | * [Join the Postman newsletter](https://sendfox.com/lp/m74j2r) 19 | 20 | ## Learn even more 21 | 22 | * [💻 Postman Complete course for API testing - with FreeCodeCamp discount](https://www.udemy.com/course/postman-the-complete-guide/?couponCode=FREECODECAMP_2023) 23 | -------------------------------------------------------------------------------- /course-notes.md: -------------------------------------------------------------------------------- 1 | # Postman Beginner's Course - API Test Automation 2 | 3 | ## Unit 1 - Introduction to API testing 4 | 5 | ### Lesson 1 - Welcome 6 | 7 | - this course is ideal if you already know how to use Postman for creating requests 8 | - in this course you will learn: 9 | - write API tests in Postman 10 | - automate test execution with various Postman tools (Collection Runner, Postman CLI) 11 | - running tests with the help of the CI/CD tool Github Actions 12 | - Postman offers an official badge for completing this course and doing all assignments 13 | 14 | #### 📚 Resources 15 | 16 | * [Say THANK YOU for this course by subscribing on YouTube](https://www.youtube.com/@vdespa?sub_confirmation=1) 17 | * [Check out Postman's YouTube channel](https://www.youtube.com/@postman?sub_confirmation=1) 18 | * [Join the Postman newsletter](https://sendfox.com/lp/m74j2r) 19 | 20 | 21 | ### Lesson 2 - Introduction to the project 22 | 23 | - before we can automate the testing of an API, we first need to do a manual test 24 | - create a new public workspace that will store all collections used during the course 25 | - to run or make changes to a collection from a workspace that isn't yours, you need to make a copy of it (create a fork) 26 | 27 | #### 📚 Resources 28 | 29 | * [Open Postman on the web](https://go.postman.co/build) 30 | * [Postman workspace - FORK from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 31 | 32 | #### 🆘 Troubleshooting 33 | 34 | - if you can't get a status 200 OK reply from the server when trying the Status endpoint, check the following: 35 | - ensure you are on postman.com 36 | - if the error persists, [submit an issue](https://github.com/vdespa/automation-with-postman-course/issues/new) and add as many details as possible, including screenshots of any errors that appear. 37 | 38 | 39 | ### Lesson 3 - What is API testing 40 | 41 | - API testing involves verifying the functionality, reliability, performance, and security of an API 42 | - the goal of API testing is to identify issues and defects in the API before it is released 43 | - this course focuses on functional testing 44 | 45 | 46 | ### Lesson 4 - Manually testing the API 47 | 48 | - to automate the API tests, we first need to know how to perform them manually 49 | - the goal of this lecture is to go through all the endpoints of this API 50 | 51 | 52 | ### Lesson 5 - Using Postman variables to store secrets 53 | 54 | - regardless if the collection is public or private, it is a best practice to store secrets (like API keys, passwords, tokens) in Postman variables 55 | - Postman auth helpers can configure different types of authentication 56 | - keep in mind that authentication can be configured on one level (collection, folder) and inherited 57 | 58 | 59 | ### Lesson 6 - Writing scripts in Postman 60 | 61 | - automated testing is a way to check if the API works correctly by letting Postman run tests 62 | - we write scripts to automate the testing of the API 63 | - API tests are written using scripts 64 | - Postman uses JavaScript for writing tests 65 | - don't confuse JavaScript with Java, as they are two different programming languages 66 | - console.log can be used to write a message to the Postman console: `console.log("Hello from the Tests!");` 67 | 68 | 69 | ### Lesson 7 - Use the Postman console for debugging scripts 70 | 71 | - the Postman console can be used to view any information about the request and the response (URL, headers, body) 72 | - any messages logged using console.log can also be seen there 73 | - to clear the Postman console of entries you can use the "Clear" button or use the following script: `console.clear()` 74 | 75 | 76 | ### Lesson 8 - Writing an API test 77 | 78 | - the following script can be added to the "Tests" to check if the response status code is 200 OK. 79 | 80 | ```javascript 81 | pm.test("Status code is 200", function () { 82 | pm.response.to.have.status(200); 83 | }); 84 | ``` 85 | 86 | ### Lesson 9 - JavaScript basics 87 | 88 | - to write tests in Postman, you need to know some JavaScript basics 89 | - these are the most important concepts you need to be familiar with: 90 | - variables and their scope 91 | - data types including objects and arrays 92 | - functions 93 | 94 | ### Lesson 10 - JavaScript basics - Variables 95 | 96 | - variables are like containers that store data 97 | - you can define a variable named "name" with the value "jamie" and log the value to the console like this: 98 | 99 | ```javascript 100 | let name = "Jamie"; 101 | console.log(name); 102 | ``` 103 | 104 | ### Lesson 11 - JavaScript basics - Variable scopes 105 | 106 | - variables defined within a block statement are available only to code within that block 107 | - variables defined outside of a block statement are in the global scope can be accessed from anywhere 108 | 109 | #### 📚 Resources 110 | 111 | * [let - JavaScript - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) 112 | * [block statement - JavaScript - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block) 113 | 114 | ### Lesson 12 - JavaScript basics - Data types 115 | 116 | - JavaScript has various data types to represent information 117 | - the most likely data types you need to know about are: string, number, boolean, object, and array 118 | - if you are unsure of a data type you can use the `typeof` operator. Example: `console.log(typeof "John"); ` 119 | - example of an object containing various data types: 120 | 121 | ```javascript 122 | let person = { 123 | name: "Jake", // string 124 | age: 29, //number 125 | isAdult: true, // boolean 126 | 'e-mail': 'jake@example.com', // string 127 | hobbies: ['reading', 'traveling', 'gardening', 'cooking'] // array of strings 128 | }; 129 | ``` 130 | 131 | #### 📚 Resources 132 | 133 | [typeof - JavaScript - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) 134 | 135 | 136 | ### Lesson 13 - JavaScript basics - Functions 137 | 138 | - functions are fundamental concepts in programming: 139 | - they are blocks of code designed to perform specific tasks 140 | - they allow us to organize and reuse code effectively 141 | - A simple function definition in JavaScript: 142 | 143 | ```javascript 144 | function greet() { 145 | console.log("Hello from Postman!"); 146 | } 147 | ``` 148 | - a function needs to be invoked to work, using the following example syntax: greet() 149 | - functions can take inputs in the form of arguments: 150 | 151 | ```javascript 152 | function greet(name) { 153 | console.log("Hello from Postman, " + name + "!"); 154 | } 155 | greet('Valentin'); 156 | ``` 157 | 158 | - using console.log in a function is primarily used for debugging and doesn't mean the function returns a value 159 | - the `return` statement specifies the value that a function should return after executing. 160 | - using `return` is necessary when you want to use the result of a function in another part of your code: 161 | 162 | ```javascript 163 | function add(a, b) { 164 | let sum = a + b; 165 | console.log(sum); 166 | return sum; 167 | } 168 | 169 | console.log("The sum is: " + add(1,2)); 170 | ``` 171 | 172 | 173 | ### Lesson 14 - JavaScript basics - Methods 174 | 175 | - you can also define a function inside an object: 176 | 177 | ```javascript 178 | let person = { 179 | firstName: "Jake", 180 | age: 29, 181 | isAdult: true, 182 | 'e-mail': 'jake@example.com', 183 | hobbies: ['reading', 'traveling', 'gardening', 'cooking'], 184 | greet: function(name) { 185 | console.log('Hello from Postman ' + name + '. My name is ' + this.firstName); 186 | } 187 | }; 188 | person.greet('Jake'); 189 | ``` 190 | 191 | - when a function is defined inside an object, we call it a method 192 | 193 | 194 | 195 | ### Lesson 15 - JavaScript basics - Callback functions 196 | 197 | - callback functions are an essential concept in JavaScript programming and allow for more efficient and flexible code. 198 | - functions can be stored in variables: 199 | 200 | ```javascript 201 | const sayHello = function() { 202 | console.log('Hello'); 203 | } 204 | ``` 205 | - functions without a name are called anonymous functions and can only be called by referencing the variable they're stored in, e.g., sayHello. 206 | - functions can be passed as arguments to another function: 207 | 208 | ```javascript 209 | function doSomething(someFunction) { 210 | someFunction(); // function is invoked here 211 | } 212 | 213 | doSomething(sayHello); // function passed as an argument 214 | ``` 215 | 216 | - functions can be defined directly inside another function: 217 | 218 | ```javascript 219 | function doSomething(someFunction) { 220 | someFunction(); 221 | } 222 | 223 | doSomething(function() { 224 | sayHello(); 225 | }); 226 | ``` 227 | 228 | - this syntax is used in Postman for writing tests 229 | 230 | 231 | 232 | ### Lesson 16 - JSON format 233 | 234 | - most APIs use the JSON format 235 | - JSON and JavaScript objects are not the same 236 | 237 | #### 📚 Resources 238 | 239 | [JSON - JavaScript - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) 240 | 241 | 242 | ### Lesson 17 - Accessing data from the response body 243 | 244 | - to access any information from a JSON response body, you first need to parse the response: 245 | 246 | ```javascript 247 | const response = pm.response.json(); 248 | console.log(response); 249 | ``` 250 | 251 | 252 | ### Lesson 18 - Passing data from one request to the other 253 | 254 | - using Postman variables is a good way to reduce duplication and to be able to control multiple requests 255 | - using Postman variables reduces or eliminates the need to copy/paste data or to manually reconfigure requests 256 | 257 | 258 | ### Lesson 19 - Setting Postman variables from scripts 259 | 260 | - JavaScript variables are not the same as Postman variables 261 | - JavaScript variables are scoped only to the script where they are defined and any variable set from there is not persisted 262 | - Postman variables useful for: 263 | - storing settings and persisting data in the long term, such as the baseUrl, API key, or other details 264 | - passing data between requests 265 | - it is possible to create or update a Postman collection variable from a script: 266 | 267 | ```javascript 268 | pm.collectionVariables.set('firstName', 'Jamie'); 269 | ``` 270 | 271 | ### Lesson 20 - Assignment #1 272 | 273 | - fork the Assignment #1 collection and follow the instructions from the documentation 274 | 275 | #### 📚 Resources 276 | 277 | * [Postman workspace - FORK Assignment #1 from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 278 | * [Postman community](https://community.postman.com/) 279 | 280 | 281 | ## Unit 2 - Writing API tests 282 | 283 | ### Lesson 1 - Unit overview 284 | 285 | - we have a Postman collection that we can run request by request from the beginning to the end. 286 | - in this unit, we will focus on writing tests for the API 287 | 288 | ### Lesson 2 - Test structure in Postman 289 | 290 | - the basic structure of a test in Postman is the following: 291 | 292 | ```javascript 293 | pm.test('Name of the test', function () { 294 | // assertions 295 | }); 296 | ``` 297 | 298 | - assertions can be written with `pm.expect`: 299 | 300 | ```javascript 301 | pm.expect(1).to.eql(1); - test passes 302 | pm.expect(1).to.eql(2); - test fails 303 | ``` 304 | 305 | ### Lesson 3 - Making assertions about the status code 306 | 307 | - an alternative way of writing a status code test using `pm.expect`: 308 | 309 | ```javascript 310 | pm.test('Status is 200', function () { 311 | pm.expect(pm.response.status).to.eql(200); 312 | }); 313 | ``` 314 | 315 | ### Lesson 4 - Assignment #2 316 | 317 | - fork the Assignment #2 collection and follow the instructions from the documentation 318 | 319 | #### 📚 Resources 320 | 321 | * [Postman workspace - FORK from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 322 | 323 | 324 | ### Lesson 5 - Asserting the response body is JSON 325 | 326 | - an API may return a response that is not JSON 327 | - before trying to parse the response, it is best to check if the response body is JSON. 328 | 329 | ```javascript 330 | pm.test("Response body is JSON", () => { 331 | pm.response.to.be.json; 332 | }); 333 | ``` 334 | 335 | ### Lesson 6 - Writing simple assertions against the response body 336 | 337 | - it is important to assert different aspects of the response body 338 | - example checking the product name which is a string: 339 | 340 | ```javascript 341 | pm.test("Product is Espresso", () => { 342 | const response = pm.response.json(); 343 | pm.expect(response.name).to.eql('Espresso'); 344 | }); 345 | ``` 346 | 347 | - example checking a boolean value: 348 | 349 | ```javascript 350 | pm.test("Product is in stock", () => { 351 | const response = pm.response.json(); 352 | pm.expect(response.isAvailable).to.eql(true); 353 | pm.expect(response.isAvailable).to.be.true; 354 | }); 355 | ``` 356 | 357 | - example where the expected value is a Postman variable: 358 | 359 | ```javascript 360 | pm.expect(response.id).to.eql(pm.collectionVariables.get('productId')); 361 | ``` 362 | 363 | ### Lesson 7 - Asserting the data-type of a property 364 | 365 | - instead of checking for a specific value, it is possible to check if a property exists 366 | - example checking if the response as a property called `name`: 367 | 368 | ```javascript 369 | expect(response).to.have.property('name'); 370 | ``` 371 | 372 | - example checking if a property of the response is a number 373 | - 374 | ```javascript 375 | expect(response.price).to.be.a('number'); 376 | ``` 377 | 378 | #### 📚 Resources 379 | 380 | [Chai Assertion Library](https://www.chaijs.com/api/bdd/) 381 | 382 | 383 | ### Lesson 8 - Using Postman random variables in assertions 384 | 385 | ### Lesson 9 - Regular expressions in tests 386 | 387 | - it is possible to use regular expressions in assertions: 388 | 389 | ```javascript 390 | pm.expect(response.id).to.match(/^[A-Z0-9]{9}$/); 391 | ``` 392 | 393 | ### Lesson 10 - Assignment #3 - Write API tests against the response body 394 | 395 | - fork the Assignment #3 collection and follow the instructions from the documentation 396 | 397 | #### 📚 Resources 398 | 399 | * [Postman workspace - FORK Assignment #3 from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 400 | 401 | ### Lesson 11 - JSON schema validations 402 | 403 | ```javascript 404 | pm.test('Schema is valid', function () { 405 | const schema = { 406 | "type": "object", 407 | "properties": { 408 | "id": { 409 | "type": "string" 410 | }, 411 | "clientId": { 412 | "type": "string" 413 | }, 414 | "created": { 415 | "type": "string", 416 | }, 417 | "customerName": { 418 | "type": "string" 419 | }, 420 | "products": { 421 | "type": "array" 422 | } 423 | } 424 | }; 425 | pm.response.to.have.jsonSchema(schema); 426 | }); 427 | ``` 428 | 429 | ### Lesson 12 - Using Postman mock servers 430 | 431 | - using Postman mock servers is an effective way to test if the tests written will fail 432 | 433 | ### Lesson 13 - Advanced JSON schema validation 434 | 435 | ``` 436 | { 437 | "type": "object", 438 | "properties": { 439 | "id": { 440 | "type": "string", 441 | "pattern": "^[A-Z0-9]{9}$" 442 | }, 443 | "clientId": { 444 | "type": "string", 445 | "pattern": "^[a-zA-Z0-9]{9}$" 446 | }, 447 | "created": { 448 | "type": "string", 449 | "format": "date-time" 450 | }, 451 | "customerName": { 452 | "type": "string" 453 | }, 454 | "products": { 455 | "type": "array", 456 | "items": { 457 | "type": "object", 458 | "properties": { 459 | "id": { 460 | "type": "integer" 461 | }, 462 | "quantity": { 463 | "type": "integer", 464 | "minimum": 1 465 | } 466 | }, 467 | "required": ["id", "quantity"] 468 | } 469 | } 470 | }, 471 | "required": ["id", "clientId", "created", "customerName", "products"] 472 | } 473 | ``` 474 | 475 | ### Lesson 14 - Common pitfalls with JSON schema validation 476 | 477 | #### 📚 Resources 478 | 479 | * [Understanding JSON Schema](https://json-schema.org/understanding-json-schema/) 480 | 481 | 482 | ### Lesson 15 - Assignment #4 - Write a JSON schema 483 | 484 | - fork the Assignment #4 collection and follow the instructions from the documentation 485 | 486 | #### 📚 Resources 487 | 488 | * [Postman workspace - FORK Assignment #4 from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 489 | 490 | 491 | ### Lesson 16 - Testing response headers 492 | 493 | ```javascript 494 | pm.expect(pm.response.headers.get('X-Powered-By')).to.eql('Express'); 495 | ``` 496 | 497 | 498 | ## Unit 3 - Automation first steps 499 | 500 | ### Lesson 2 - Collection runner 501 | 502 | - the Postman collection runner is the first step toward testing if an automated run is possible 503 | - the Postman collection runner is a useful tool for debugging 504 | 505 | ### Lesson 3 - Scheduled runs 506 | 507 | - runs are executed on the Postman Cloud 508 | - no need to keep Postman open or your computer on during the run 509 | - an excellent tool for API monitoring after deployment, ensuring it continues to function as expected. 510 | - variable issues: if a variable isn't being resolved during the run, ensure that the variable has been correctly defined in the selected environment. 511 | - best suited for APIs that are publicly accessible. 512 | - limited use for pre-production environments which are typically not publicly accessible. 513 | - Important: don't put secrets in the initial value of a Postman variable if the workspace is public 514 | 515 | ### Lesson 4 - Postman CLI 516 | 517 | - CLI tools are necessary for running Postman collections without human intervention and on CI/CD servers like Jenkins, Github Actions, or GitLab 518 | - Postman CLI is a command line tool for running Postman collections 519 | 520 | 521 | #### 📚 Resources 522 | 523 | * [Installing the Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-installation/) 524 | 525 | 526 | ### Lesson 5 - Running a collection using Postman CLI 527 | 528 | - when selecting "Run collection" > "Automate runs via CLI", Postman will provide the necessary commands needed to run the collection 529 | - the first step is authentication with the command `postman login` (this requires a Postman API key) 530 | - the second step is running the collection with the command `postman collection run` 531 | 532 | ### Lesson 6 - Postman CLI options 533 | 534 | - For advanced Postman CLI configurations, you can specify additional command options 535 | 536 | #### 📚 Resources 537 | 538 | * [Postman CLI command options](https://learning.postman.com/docs/postman-cli/postman-cli-options/) 539 | 540 | 541 | ## Unit 4 - Integrating Postman tests in CI/CD 542 | 543 | ### Lesson 1 - Unit overview 544 | 545 | - this is the final unit that focuses on integrating Postman tests in Continuous Integration/Continuous Deployment (CI/CD) pipelines 546 | - the objective is to automate API testing, ensuring APIs are continuously validated 547 | 548 | ### Lesson 2 - What is CI/CD? 549 | 550 | - CI/CD stands for Continuous Integration and Continuous Deployment which is a practice in software development 551 | - CI/CD aims to make software development process faster and more reliable 552 | - Continuous Integration involves automatic testing of code changes and helps in detecting issues early 553 | - After CI, the Continuous Deployment (CD) pipeline begins 554 | - CD typically first deploys software to a test environment for testing purposes 555 | - if tests pass, changes are automatically deployed to production 556 | - Postman tests can be run at two points: a. after deploying API to pre-production environment; b. after deploying to the production environment 557 | - Postman CLI can automate running collections and tests in the CD pipeline 558 | 559 | ### Lesson 3 - CI/CD providers 560 | 561 | - there isn’t a single “best” tool for CI/CD 562 | - popular solutions for CI/CD include Jenkins, GitLab, Circle CI, GitHub Actions, … 563 | - once you understand how to use Postman CLI, integration with any other CI/CD server is relatively easy 564 | - to use GitHub Action you need to sign-up for a free GitHub account 565 | 566 | * [GitHub account](https://github.com/signup?source=login) 567 | 568 | ### Lesson 4 - Running Postman tests with GitHub Actions 569 | 570 | - Create a new repository and make it public 571 | - create a new workflow and grab the pipeline configuration from Postman 572 | - store API key privately & reference it in the pipeline configuration 573 | 574 | ### Lesson 5 - Assignment #5 - Run Postman in a CI/CD server 575 | 576 | - fork the Assignment #5 collection and follow the instructions from the documentation 577 | 578 | #### 📚 Resources 579 | 580 | * [Postman workspace - FORK Assignment #5 from here](https://www.postman.com/valentins-team/workspace/test-automation-valentino-s-artisan-coffee-house-api/overview) 581 | 582 | 583 | ### Lesson 6 - Running Postman tests in other CI/CD tools 584 | 585 | - Postman can generate pipeline configurations for the following tools: Jenkins, Bitbucket Pipelines, CircleCI, GitLab, Azure Pipelines, Travis CI 586 | - test executions in the CI/CD are ingested into the workspace reports 587 | 588 | ### Lesson 7 - Collaboration within a Postman workspace 589 | 590 | - Postman is all about collaboration. Most of the time, you would want to create a team in Postman and use team workspaces 591 | - Important: once you create a team workspace, your "Public" workspace will transform in a "Personal" workspace; make sure to change it back to "Public" 592 | 593 | ### Lesson 8 - Claim your badge 594 | 595 | - ensure you have completed all assignments 596 | - ensure that your workspace is "Public" and not "Personal" 597 | - to claim your Postman badge, follow the instructions from the collection named "Claim your badge" 598 | 599 | ### Lesson 9 - Conclusion 600 | 601 | - Congrats! You have completed the API test automation course with Postman. 602 | 603 | 604 | #### Fun facts about this project 605 | 606 | * 🕐 142 hours of work (and counting) 607 | * ☕️ 56 coffees 608 | * ∞ amount of fun! 609 | 610 | #### 💬 Let's stay in touch 611 | 612 | * [Say THANK YOU for this course by subscribing on YouTube](https://www.youtube.com/@vdespa?sub_confirmation=1) 613 | * [Subscribe to Postman on YouTube](https://www.youtube.com/@postman?sub_confirmation=1) 614 | * [Follow me on Twitter](https://twitter.com/vdespa) 615 | * [Let's connect on LinkedIn](https://www.linkedin.com/in/vdespa/) 616 | 617 | --------------------------------------------------------------------------------