Skip to content

Commit

Permalink
Merge pull request #9 from naodeng/main
Browse files Browse the repository at this point in the history
add demo for multi env support
  • Loading branch information
dengnao-tw authored Nov 17, 2023
2 parents f7997d3 + ca740fb commit 0947370
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Test with pytest
run: |
pytest
ENV=dev pytest
- name: Archive Pytest test report
uses: actions/upload-artifact@v3
Expand Down
22 changes: 5 additions & 17 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@
import json
import os

# def env_config(request):
# environment = request.config.getoption("--environment")
# if environment == "dev":
# # 在开发环境中执行测试
# pass
# elif environment == "test":
# # 在测试环境中执行测试
# pass
# elif environment == "prod":
# # 在生产环境中执行测试
# pass


@pytest.fixture(scope="session")
def env_config(request):
# 根据命令行参数选择环境配置文件
# get config file from different env
env = os.getenv('ENV', 'dev')
with open(f'config/{env}_config.json', 'r') as config_file:
config = json.load(config_file)
Expand All @@ -27,17 +15,17 @@ def env_config(request):

@pytest.fixture(scope="session")
def env_request_data(request):
# 根据命令行参数选择环境request_data文件
# get request data file from different env
env = os.getenv('ENV', 'dev')
with open(f'data/{env}_request_test_data.json', 'r') as request_data_file:
with open(f'data/{env}_request_data.json', 'r') as request_data_file:
request_data = json.load(request_data_file)
return request_data


@pytest.fixture (scope="session")
def env_response_data(request):
# 根据命令行参数选择环境response_data文件
# get response data file from different env
env = os.getenv('ENV', 'dev')
with open(f'data/{env}_response_test_data.json', 'r') as response_data_file:
with open(f'data/{env}_response_data.json', 'r') as response_data_file:
response_data = json.load(response_data_file)
return response_data
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"postAPI":{
"title": "foo",
"body": "bar",
"userId": 1,
"userId": "1",
"id": 101
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"postAPI":{
"title": "foo",
"body": "bar",
"userId": 1,
"userId": "1",
"id": 101
}
}
6 changes: 3 additions & 3 deletions test_case/test_demo_data_driving.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
response_data = json.load(json_file)


class TestPytestDemo:
class TestPytestDataDrivingDemo:

def test_get_demo(self):
def test_get_demo_data_driving(self):
host = config.get("host")
get_api = config.get("getAPI")
get_api_response_data = response_data.get("getAPI")
Expand All @@ -26,7 +26,7 @@ def test_get_demo(self):
assert response.status_code == 200
assert response.json() == get_api_response_data

def test_post_demo(self):
def test_post_demo_data_driving(self):
host = config.get("host")
post_api = config.get("postAPI")
post_api_request_data = request_data.get("postAPI")
Expand Down
26 changes: 26 additions & 0 deletions test_case/test_demo_multi_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import requests
import json


class TestPytestMultiEnvDemo:

def test_get_demo_multi_env(self, env_config, env_request_data, env_response_data):
host = env_config["host"]
get_api = env_config["getAPI"]
get_api_response_data = env_response_data["getAPI"]
# send request
response = requests.get(host+get_api)
# assert
assert response.status_code == 200
assert response.json() == get_api_response_data

def test_post_demo_multi_env(self, 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"]
post_api_response_data = env_response_data["postAPI"]
# send request
response = requests.post(host + post_api, post_api_request_data)
# assert
assert response.status_code == 201
assert response.json() == post_api_response_data

0 comments on commit 0947370

Please sign in to comment.