diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 2647371..1a4da3c 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -30,7 +30,7 @@ jobs: - name: Test with pytest run: | - pytest + ENV=dev pytest - name: Archive Pytest test report uses: actions/upload-artifact@v3 diff --git a/conftest.py b/conftest.py index 82e3c07..88d0075 100644 --- a/conftest.py +++ b/conftest.py @@ -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) @@ -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 diff --git a/data/dev_request_test_data.json b/data/dev_request_data.json similarity index 100% rename from data/dev_request_test_data.json rename to data/dev_request_data.json diff --git a/data/dev_response_test_data.json b/data/dev_response_data.json similarity index 95% rename from data/dev_response_test_data.json rename to data/dev_response_data.json index 2de9c28..38d7f8b 100644 --- a/data/dev_response_test_data.json +++ b/data/dev_response_data.json @@ -8,7 +8,7 @@ "postAPI":{ "title": "foo", "body": "bar", - "userId": 1, + "userId": "1", "id": 101 } } \ No newline at end of file diff --git a/data/prod_request_test_data.json b/data/prod_request_data.json similarity index 100% rename from data/prod_request_test_data.json rename to data/prod_request_data.json diff --git a/data/prod_response_test_data.json b/data/prod_response_data.json similarity index 95% rename from data/prod_response_test_data.json rename to data/prod_response_data.json index 2de9c28..38d7f8b 100644 --- a/data/prod_response_test_data.json +++ b/data/prod_response_data.json @@ -8,7 +8,7 @@ "postAPI":{ "title": "foo", "body": "bar", - "userId": 1, + "userId": "1", "id": 101 } } \ No newline at end of file diff --git a/test_case/test_demo_data_driving.py b/test_case/test_demo_data_driving.py index 6a55161..b3bd362 100644 --- a/test_case/test_demo_data_driving.py +++ b/test_case/test_demo_data_driving.py @@ -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") @@ -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") diff --git a/test_case/test_demo_multi_environment.py b/test_case/test_demo_multi_environment.py new file mode 100644 index 0000000..ae57728 --- /dev/null +++ b/test_case/test_demo_multi_environment.py @@ -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