Skip to content

taurus5650/automation_api_test_pytest

Repository files navigation

Testing Restful API with Python Pytest

Purpose

Directory Structure

git ls-tree -r --name-only HEAD | tree --fromfile
.
├── .gitignore
├── README.md
├── business
│   ├── api_request.py
│   ├── books
│   │   ├── books_api.py
│   │   └── books_database.py
│   ├── database_execution.py
│   └── users
│       ├── users_api.py
│       └── users_database.py
├── configurations
│   ├── __init__.py
│   ├── api_domain.py
│   └── database.py
├── conftest.py
├── deployments
│   ├── Dockerfile
│   ├── Jenkinsfile
│   └── job_configurations.json
├── logger.py
├── requirements.txt
└── test_suites
    ├── test_books
    │   └── test_get_books_id.py
    └── test_users
        ├── test_get_users.py
        └── test_post_users.py

Step-by-step

Option 1: Build up a simple Docker

Input the command

docker build -t pytest_learn_image -f ./deployments/Dockerfile .

Option 2: Build up Jenkins Docker

Ref: How to Run Jenkins Container as Systemd Service with Docker

  1. Build a Jenkins run in Docker
  • --name jenkins : Container naming
  • -p 8080:8080 : Container 8080 port mapping to localhost 8080 port, which for jenkins administrator login.
  • -p 50000:50000 Container 50000 port mapping to localhost 50000 port, which for jenkins slave node's JNLP (Java Web Start) port.
  • -v ~/jenkins_home:/var/jenkins_home : Build up docker volume. Container /var/jenkins_home mapping to localhost ~/jenkins_home.
  • -v /var/run/docker.sock:/var/run/docker.sock : Let containers can use docker daemon.
$ docker run --name jenkins -p 8080:8080 -p 50000:50000 -v ~/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts
  1. Started the container and command for installation
$ apt-get update
$ apt install python3
$ apt install python3.11-venv
$ apt-get install -y wget
$ wget https://github.com/allure-framework/allure2/releases/download/2.14.0/allure-2.14.0.zip
$ unzip allure-2.14.0.zip
$ export PATH=$PATH:/path/to/allure-2.14.0/bin

Note: If docker cli encountering no permission

$ docker exec -u 0 -it {#CONTAINER_ID} /bin/bash
  1. Launch browser way to http://localhost:8080/
    a. Unlock the Jenkins with password. The password form container's logs when u started the container.
    b. Install suggested plugins. Ref. How to Run Jenkins Container as Systemd Service with Docker

  2. Change Jenkinsfile configurations
    a. GIT_REPO : Ur repo
    b. _GIT_CREDENTIALS_ID : From Jenkins credentials setting Ref How To Add Git Credentials In Jenkins
    Ref. Configuring the Git Credentials in Jenkins Jenkinsfile_config.png

  3. Change job configurations In this case, the pipeline job will separate by services.
    Thus, We may create the pipeline by services.
    Before create the Jenkins pipeline, we need to add job in job_configuration.json. \

└── test_suites
    ├── test_books
    │   └── test_get_books_id.py
    └── test_users
        ├── test_get_users.py
        └── test_post_users.py

job_config_json.png

  1. Build Jenkins pipeline, select [new items] build a new job. jenkins_new_item.png

  2. Give a job naming and select pipeline jenkins_choose_pipeline.png

  3. Do the config

  1. Okieee, then run the job. Click [Build parameters] jenkins_job_builds_params.png

  2. Input parameters (optional, if didn't input then run as default) job_run_build.png

  3. Job complete jenkins_job_complete.png

  4. May way to allure report to check the result :) allure_report.png