BHOL801 - 接口测试生命周期管理

demo 1 - add swagger ui into node.js express

git clone git@github.com:idcf-boat-house/boathouse-sampleapi.git

  1. add swagger.json
  2. update server.js
  1. // line 9
  2. const swaggerUi = require('swagger-ui-express'),
  3. swaggerDocument = require('./swagger.json');
  4. // before app.listen
  5. app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
  1. start the application and open http://localhost:3000/api-docs/
  1. npm install
  2. npm start

demo 2 - use postman to test boathouse-calculator

open postman

  1. add new collection boathouse-calcuator-tests
  2. add api requests and run the requests
    • adds two positive integers {{host}}/arithmetic?operation=add&operand1=21&operand2=21
    • adds zero to an integer {{host}}/arithmetic?operation=add&operand1=42&operand2=0
  3. add the following tests
  1. //adds two positive integers
  2. pm.test("Status code is 200", function () {
  3. pm.response.to.have.status(200);
  4. });
  5. pm.test("Should reture 42", function () {
  6. var jsonData = pm.response.json();
  7. pm.expect(jsonData.result).to.eql('42');
  8. });
  9. //adds zero to an integer
  10. pm.test("Status code should be 200", function(){
  11. pm.response.to.have.status(200);
  12. });
  13. pm.test("Expected result should be 42", ()=>{
  14. const reponseJson = pm.response.json();
  15. pm.expect(reponseJson.result).to.eql(42);
  16. });

03 - Run postman collections with newman

  1. # install newman to run postman collections in command line https://www.npmjs.com/package/newman
  2. npm install -g newman
  3. # install newman-reporter-junit to export test report in juntest format https://www.npmjs.com/package/newman-reporter-junitfull
  4. npm install -g newman-reporter-junitfull
  5. ## newman sample command
  6. newman run postman/boathouse-calculator.postman_collection.json -e postman/local-dev.postman_environment.json
  7. ## newman sample command with junit report export
  8. newman run postman/boathouse-calculator.postman_collection.json -e postman/local-dev.postman_environment.json -r junitfull --reporter-junitfull-export './postman/result.xml' -n 2
  1. ## run newman using the newman docker container
  2. docker run -v "${PWD}/postman:/etc/newman" -t postman/newman:alpine run boathouse-calculator.postman_collection.json -e local-dev.postman_environment.json
  3. ## with junit format report
  4. docker run -v "${PWD}/postman:/etc/newman" -t postman/newman:alpine run boathouse-calculator.postman_collection.json -e local-dev.postman_environment.json --reporters junit --reporter-junit-export 'result-docker.xml'

04 - Run postman collections with newman

update Jenkinsfile

  1. stage ('API Testing - Postman'){
  2. steps {
  3. warnError('Testing Failed'){
  4. sh 'docker run --rm -v "${PWD}/postman:/etc/newman" -t postman/newman:alpine run boathouse-calculator.postman_collection.json -e local-dev.postman_environment.json --reporters junit --reporter-junit-export "result-docker.xml"'
  5. }
  6. }
  7. post {
  8. always{
  9. echo "upload test results ..."
  10. junit 'postman/*.xml'
  11. }
  12. }
  13. }

05 - Contract Testing with Pact