Skip to content

Commit

Permalink
Merge pull request #717 from IntersectMBO/feat/pytest-allure-reports
Browse files Browse the repository at this point in the history
Add allure reporting support in backend test
  • Loading branch information
NabinKawan authored Apr 18, 2024
2 parents b4f132f + d709d4f commit 0eb2d9f
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 29 deletions.
127 changes: 101 additions & 26 deletions .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ name: Backend Test
on:
push:
paths:
- .github/workflows/test_backend.yml
- .github/workflows/test_backend.yml
# - govtool/backend
# - tests/govtool-backend

schedule:
- cron: '0 0 * * *'
- cron: "0 0 * * *"
workflow_dispatch:
inputs:
deployment:
Expand All @@ -21,29 +21,104 @@ on:
- "govtool-sanchonet.cardanoapi.io/api"

jobs:
build:
backend-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.4
cache: 'pip'

- name: Run Backend Test
working-directory: tests/govtool-backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pytest -v --github-report
env:
BASE_URL: https://${{inputs.deployment || 'staging.govtool.byron.network/api' }}
METRICS_URL: https://metrics.cardanoapi.io
METRICS_API_SECRET: "${{ secrets.METRICS_SERVER_SECRET_TOKEN }}"

# - uses: schemathesis/action@v1
# with:
# schema: "http://localhost:8080/swagger.json"
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.4
cache: "pip"

- name: Run Backend Test
working-directory: tests/govtool-backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pytest --alluredir allure-results
env:
BASE_URL: https://${{inputs.deployment || 'staging.govtool.byron.network/api' }}
METRICS_URL: https://metrics.cardanoapi.io
METRICS_API_SECRET: "${{ secrets.METRICS_SERVER_SECRET_TOKEN }}"

- name: Upload report
uses: actions/upload-artifact@v3
if: always()
with:
name: allure-results
path: tests/govtool-backend/allure-results

publish-report:
runs-on: ubuntu-latest
if: always()
needs: backend-tests
steps:
- uses: actions/checkout@v4
- name: Download results
uses: actions/download-artifact@v3
with:
name: allure-results
path: allure-results

- name: Get Allure history
uses: actions/checkout@v4
continue-on-error: true
with:
ref: gh-pages
path: gh-pages
repository: ${{vars.GH_PAGES}}
token: ${{secrets.PERSONAL_TOKEN}}

- name: Register report
id: register-project
if: ${{success()}}
run: |
chmod +x ./register_report.sh
./register_report.sh
- if: steps.register-project.outputs.project_exists != 'true'
uses: JamesIves/github-pages-deploy-action@v4
with:
token: ${{ secrets.PERSONAL_TOKEN }}
repository-name: ${{vars.GH_PAGES}}
branch: gh-pages
folder: project

- name: Generate report details
id: report-details
run: |
chmod +x ./generate_report_details.sh
./generate_report_details.sh
- name: Build report
uses: simple-elf/allure-report-action@master
if: always()
id: allure-report
with:
allure_results: allure-results
gh_pages: gh-pages/${{env.REPORT_NAME}}
allure_report: allure-report
allure_history: allure-history
keep_reports: 2000
report_url: ${{steps.report-details.outputs.report_url}}
github_run_num: ${{steps.report-details.outputs.report_number}}

- name: Generate Latest Report
run: |
chmod +x ./generate_latest_report_redirect.sh
./generate_latest_report_redirect.sh ${{steps.report-details.outputs.report_number}}
- name: Deploy report to Github Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
token: ${{ secrets.PERSONAL_TOKEN }}
repository-name: ${{vars.GH_PAGES}}
branch: gh-pages
folder: build
target-folder: ${{ env.REPORT_NAME }}

env:
REPORT_NAME: backend
GH_PAGES: ${{vars.GH_PAGES}}
18 changes: 18 additions & 0 deletions generate_latest_report_redirect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

REPORT_NUMBER="$1"

mkdir -p build
cp -r allure-history/*[^index.html] build/
allure_report_path=$(basename "$GH_PAGES")

cat <<EOF > build/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=/$allure_report_path/$REPORT_NAME/$REPORT_NUMBER">
<title>Redirecting...</title>
</head>
</html>
EOF
11 changes: 11 additions & 0 deletions generate_report_details.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

if [[ ! -d "gh-pages/$REPORT_NAME" ]]; then
latest_number=0
else
gh_pages_content=$(ls "gh-pages/$REPORT_NAME/")
latest_number=$(echo "$gh_pages_content" | grep -Eo '[0-9]+' | sort -nr | head -n 1)
fi

echo "::set-output name=report_number::$((latest_number+1))"
echo "::set-output name=report_url::https://$(dirname "$GH_PAGES").github.io/$(basename "$GH_PAGES")/$REPORT_NAME"
15 changes: 15 additions & 0 deletions register_report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

PROJECT_DIR="project"
PROJECT_FILE="projects.txt"

mkdir -p "$PROJECT_DIR"
cp -r gh-pages/* "$PROJECT_DIR" || true

if grep -q "$REPORT_NAME" "$PROJECT_DIR/$PROJECT_FILE"; then
echo "Project already exists"
echo "::set-output name=project_exists::true"
else
echo "$REPORT_NAME" >> "$PROJECT_DIR/$PROJECT_FILE"
echo "::set-output name=project_exists::false"
fi
1 change: 1 addition & 0 deletions tests/govtool-backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Thumbs.db

# Other
*.bak
reports/
2 changes: 1 addition & 1 deletion tests/govtool-backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest==7.4.0
pytest-github-report==0.0.1
python-dotenv==1.0.0
requests==2.31.0
allure-pytest==2.13.5
4 changes: 3 additions & 1 deletion tests/govtool-backend/test_cases/test_ada_holder.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from models.TestData import AdaHolder, Delegation
import allure

@allure.story("AdaHolder")
def test_ada_delegation(govtool_api, ada_holder_delegate_to_drep):
print(ada_holder_delegate_to_drep)
response = govtool_api.ada_holder_get_current_delegation(ada_holder_delegate_to_drep["stakeKey"])
resp = response.json()
if resp:
assert ada_holder_delegate_to_drep["drepId"] in resp


@allure.story("Drep")
def test_check_voting_power(govtool_api, ada_holder_delegate_to_drep):
response = govtool_api.ada_holder_get_voting_power(ada_holder_delegate_to_drep["stakeKey"])
ada_holder_voting_power = response.json()
Expand Down
7 changes: 6 additions & 1 deletion tests/govtool-backend/test_cases/test_drep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from models.TestData import Drep, VoteonProposal, Vote, Proposal
import allure

@allure.story("Drep")
def validate_drep_list(drep_list: [Drep]) -> bool:
for item in drep_list:
if not isinstance(item, dict):
Expand All @@ -10,6 +12,7 @@ def validate_drep_list(drep_list: [Drep]) -> bool:
return False
return True

@allure.story("Drep")
def validate_voteonproposal_list(voteonproposal_list: [VoteonProposal]) -> bool:
for item in voteonproposal_list:
if not isinstance(item, dict):
Expand All @@ -34,12 +37,13 @@ def validate_voteonproposal_list(voteonproposal_list: [VoteonProposal]) -> bool:
return True


@allure.story("Drep")
def test_list_drep(govtool_api):
response = govtool_api.drep_list()
drep_list = response.json()
validate_drep_list(drep_list)


@allure.story("Drep")
def test_initialized_getVotes( govtool_api, registered_drep):
response = govtool_api.drep_getVotes(registered_drep["drepId"])
validate_voteonproposal_list(response.json())
Expand All @@ -49,6 +53,7 @@ def test_initialized_getVotes( govtool_api, registered_drep):
assert len(proposals)==0


@allure.story("Drep")
def test_initialized_getVotingPower(govtool_api, registered_drep):
response = govtool_api.drep_get_voting_power(registered_drep["drepId"])
assert isinstance(response.json(), int)
3 changes: 3 additions & 0 deletions tests/govtool-backend/test_cases/test_proposal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from models.TestData import Proposal
import allure

@allure.story("Proposal")
def validate_proposal_list(proposal_list: [Proposal]) -> bool:
for item in proposal_list:
if not isinstance(item, dict):
Expand All @@ -13,6 +15,7 @@ def validate_proposal_list(proposal_list: [Proposal]) -> bool:
return True


@allure.story("Proposal")
def test_list_proposal(govtool_api):
response = govtool_api.proposal_list()
proposal_list = response.json()
Expand Down

0 comments on commit 0eb2d9f

Please sign in to comment.