在不同环境中使用 maven 运行 RestAssured 烟雾测试,并使用其自己唯一的 API 密钥作为标头 [英] Running RestAssured smoke tests with maven on different environments with its own unique API keys as headers
问题描述
我有一组 RestAssured 测试标记为冒烟测试,我使用 maven 通过命令行使用命令运行它们mvn test -PSmokeTests -Denv=QA
I have a set of RestAssured tests marked as smoke tests and I am using maven to run them via command line using the command mvn test -PSmokeTests -Denv=QA
env=QA 被定义为 pom.xml 中的系统属性.我明白,如果我需要在不同的环境(如 dev、staging 或 prod)上运行它,我可以将其指定为命令行参数,并且在我的测试中,我可以处理它.
The env=QA is defined as a system property in pom.xml. I understand that If I need to run this on a different env like dev or staging or prod, I can specify that as a command line argument and within my tests, I can handle it.
但是,这些环境中的每一个都需要为该环境使用不同的 API 密钥,作为 API 请求中的标头传递.我将密钥存储在单独的 headers_key 文件中并在代码中引用它们.但是我想避免这种情况,因为当我将代码推送到像 git 这样的源代码存储库时,我不希望这些键可见.
However each of these environments need to use a different API key for that environment passed as a header in the API requests. I am storing the keys in a separate headers_key file and referencing them in the code. However I want to avoid this as I dont want the keys to be visible when I push my code into a source code repo like git.
在 Maven 中是否有任何有效的方法来处理这个问题?API 密钥的存储方式是否可以根据环境获取它们,而不必将它们存储在 RestAssured 框架内的单独文件中?
Is there any efficient way of handling this within maven? Can the API keys be stored in such a way that they get picked up depending on the environment without having to store them in a separate file within the RestAssured framework?
这是我的 pom.xml 片段:
Here is my pom.xml snippet:
<profile>
<id>SmokeTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<env>${env}</env>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>SmokeTests.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
这就是我目前在 RestAssured 代码中处理它的方式:
And this is how I am currently handling it within the RestAssured code:
public static RequestSpecification getJSONRequestSpecification() {
REQUESTBUILDER = new RequestSpecBuilder();
env = System.getProperty("env");
if (env.matches("QA")) {
REQUESTBUILDER.setBaseUri(Path.BASE_URI_QA);
REQUESTBUILDER.addHeader("X-Api-Key", APIKeys.QA_API_KEY);
}
if (env.matches("Dev")) {
REQUESTBUILDER.setBaseUri(Path.BASE_URI_Dev);
REQUESTBUILDER.addHeader("X-Api-Key", APIKeys.Dev_API_KEY);
}
if (env.matches("Staging")) {
REQUESTBUILDER.setBaseUri(Path.BASE_URI_Staging);
REQUESTBUILDER.addHeader("X-Api-Key", APIKeys.Staging_API_KEY);
}
if (env.matches("Integration")) {
REQUESTBUILDER.setBaseUri(Path.BASE_URI_Int);
REQUESTBUILDER.addHeader("X-Api-Key", APIKeys.Int_API_KEY);
}
REQUESTBUILDER.setContentType(Headers.CONTENT_TYPE_JSON);
REQUEST_SPEC = REQUESTBUILDER.build();
REQUEST_SPEC.log().ifValidationFails();
return REQUEST_SPEC;
}
推荐答案
您可以使用类似于传递 env 的方式的命令行变量传递 api 密钥.
you can pass the api key using command line variables similar to how you are passing env.
mvn test -PSmokeTests -Denv=QA -DapiKey="keyvalue123"
您可以使用以下命令在您的 java 项目中访问 api 密钥
You can ccess api key inside your java project using the following command
String apiKey = System.getProperty("apiKey");
这篇关于在不同环境中使用 maven 运行 RestAssured 烟雾测试,并使用其自己唯一的 API 密钥作为标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!