From 8f616ae1a1ea03aafb7bfcc97ea7119b6f62832e Mon Sep 17 00:00:00 2001 From: Nao Date: Wed, 15 Nov 2023 18:16:37 +0800 Subject: [PATCH 1/4] Create pytest.yml --- .github/workflows/pytest.yml | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/pytest.yml diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..10d35d0 --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,45 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: + push: + branches: [ "demo" ] + pull_request: + branches: [ "demo" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -f requirements.txt + + - name: Test with pytest + run: | + pytest + + - name: Archive Pytest test report + uses: actions/upload-artifact@v3 + with: + name: SuperTest-test-report + path: report + + - name: Upload Pytest report to GitHub + uses: actions/upload-artifact@v3 + with: + name: Pytest-test-report + path: report From d77b81842689e84e5221556224dce414bd5903af Mon Sep 17 00:00:00 2001 From: Nao Date: Wed, 15 Nov 2023 18:17:49 +0800 Subject: [PATCH 2/4] Update pytest.yml --- .github/workflows/pytest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 10d35d0..ebccaa4 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -5,9 +5,9 @@ name: Python application on: push: - branches: [ "demo" ] + branches: [ "main" ] pull_request: - branches: [ "demo" ] + branches: [ "main" ] permissions: contents: read From 362709b16d54384ae547b74f4127fdc38ee08c2f Mon Sep 17 00:00:00 2001 From: Nao Date: Wed, 15 Nov 2023 18:20:14 +0800 Subject: [PATCH 3/4] Update pytest.yml --- .github/workflows/pytest.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index ebccaa4..cc5ad8a 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Python application +name: Pytest API Testing on: push: @@ -13,7 +13,7 @@ permissions: contents: read jobs: - build: + Pytes-API-Testing: runs-on: ubuntu-latest @@ -26,7 +26,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -f requirements.txt + pip install -r requirements.txt - name: Test with pytest run: | From bb894ee7e8e20fc647821a235953540b19107638 Mon Sep 17 00:00:00 2001 From: naodeng Date: Wed, 15 Nov 2023 20:51:09 +0800 Subject: [PATCH 4/4] update demo --- .gitignore | 3 ++- pytest.ini | 5 +++- requirements.txt | 2 ++ test_case/test_demo_1.py | 27 +++++++++++++++++++ .../{test_demo.py => test_demo_allure.py} | 4 +-- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test_case/test_demo_1.py rename test_case/{test_demo.py => test_demo_allure.py} (86%) diff --git a/.gitignore b/.gitignore index 4d607dd..2434902 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,5 @@ cython_debug/ #.idea/ allure-report/ -.idea/ \ No newline at end of file +.idea/ +report \ No newline at end of file diff --git a/pytest.ini b/pytest.ini index 6dcb712..558733b 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,5 @@ [pytest] -addopts = --alluredir ./allure-report \ No newline at end of file +# allure +# addopts = --alluredir ./allure-report +# pytest-html +addopts = -vs -rf --html-report=./report --title='PYTEST REPORT' --self-contained-html \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a6507f5..36c1ca6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,5 @@ pytest==7.4.3 requests==2.31.0 urllib3==2.0.7 pytest-custom-exit-code==0.3.0 +pytest-html==4.1.1 +pytest-html-reporter==0.2.9 diff --git a/test_case/test_demo_1.py b/test_case/test_demo_1.py new file mode 100644 index 0000000..0deba70 --- /dev/null +++ b/test_case/test_demo_1.py @@ -0,0 +1,27 @@ +import requests + + +class TestPytestDemo: + + def test_get_demo(self): + base_url = "https://jsonplaceholder.typicode.com" + # 发起请求 + response = requests.get(f"{base_url}/posts/1") + # 断言 + assert response.status_code == 200 + assert response.json()['userId'] == 1 + assert response.json()['id'] == 1 + + def test_post_demo(self): + base_url = "https://jsonplaceholder.typicode.com" + requests_data = { + "title": "foo", + "body": "bar", + "userId": 1 + } + # 发起请求 + response = requests.post(f"{base_url}/posts", requests_data) + # 断言 + assert response.status_code == 201 + assert response.json()['userId'] == '1' + assert response.json()['id'] == 101 \ No newline at end of file diff --git a/test_case/test_demo.py b/test_case/test_demo_allure.py similarity index 86% rename from test_case/test_demo.py rename to test_case/test_demo_allure.py index 7eba3fe..d85801b 100644 --- a/test_case/test_demo.py +++ b/test_case/test_demo_allure.py @@ -4,7 +4,7 @@ @allure.title("Test example get endpoint") @allure.description("Test the example get API endpoint") -def test_get_example_endpoint(env_config, env_request_data, env_response_data): +def test_get_example_endpoint_allure(env_config, env_request_data, env_response_data): host = env_config["host"] get_api = env_config["getAPI"] get_api_request_data = env_request_data["getAPI"] @@ -20,7 +20,7 @@ def test_get_example_endpoint(env_config, env_request_data, env_response_data): @allure.title("Test example POST endpoint") @allure.description("Test the example POST API endpoint") -def test_post_example_endpoint(env_config, env_request_data, env_response_data): +def test_post_example_endpoint_allure(env_config, env_request_data, env_response_data): host = env_config["host"] post_api = env_config["postAPI"] post_api_request_data = env_request_data["postAPI"]