测试套件环境变量

huangapple go评论74阅读模式
英文:

Test Suite Environment Variables

问题

我在 Quarkus 中遇到了一个初学者的挑战(我在 Java 中也是新手,所以如果我遇到了一个愚蠢的情况,我很抱歉)。我正在使用 ".env" 文件来存储本地开发变量,并在 "application.properties" 中设置(非机密的)生产变量。

事情进展得很顺利,但现在我正在尝试设置一个测试套件,来自 ".env" 的变量正在覆盖 "application.properties" 中的变量(即使是带有 "%test" 配置文件的变量),这使我无法同时拥有开发和测试环境。

对我来说,可以通过为不同的 %配置文件拥有单独的(源代码控制索引的)env 文件来实现,比如 ".env.testing",然后以某种方式将其分配给测试运行程序。这种模式在其他语言的测试框架中非常常见,但我找不到解决方法。

有任何想法吗?

谢谢

英文:

I'm facing a rookie challenge in Qurkus (I'm also new in Java, so sorry if I'm having a silly situation). I'm using ".env" file for local development vars and setting (non-secret) production vars in "application.properties".

Things were fine but now that I'm trying to setup a test suite, vars from ".env" are overwriting the ones in "application.properties" (even then ones with "%test" profile) & blocks me to have a development and test environment simultaneously.

To me this can be done by having a separate (source control indexed) env file for different %profiles like ".env.testing" and somehow assign it to test runner. This pattern is very common in other languages' testing frameworks, but I couldn't find a solution for that.

Have any idea?

Thanks

答案1

得分: 1

以下是翻译好的内容:

原来Quarkus在.env文件中支持不同格式的配置文件(但文档中没有提到)。因此,我们可以将我们的application.properties文件保持如下:

app.env=production

.env文件保持如下:

_DEV_APP_ENV=development
_TEST_APP_ENV=testing

更详细的文档可以在此PR中找到。

英文:

It turned out that Quarkus supports configuration profiles in .env with a different format (but it's not mentioned in the docs). So we can simply keep our application.properties file like this:

app.env=production

and the .env file like this:

_DEV_APP_ENV=development
_TEST_APP_ENV=testing

A more detailed doc can be found in this PR.

答案2

得分: 0

一个简单的方法是在您的 application.properties 文件中使用属性表达式。

例如,要设置 application.host,您可以在 application.properties 文件中使用属性表达式,如 application.host=${HOST:localhost}。如果在您的 .env 文件或 application.properties 文件中定义了名为 HOST 的属性,则将其值分配给 application.host,否则将分配默认值(在此示例中为 localhost)。

英文:

One simple way is to use property expressions in your application.properties file.

For instance, to set the application.host, you can use a property expression like application.host=${HOST:localhost} in your application.properties file. If a property named HOST is defined either in your .env file or the application.properties file, then it's value is assigned to application.host, otherwise, the default value (localhost in this example) is assigned instead.

huangapple
  • 本文由 发表于 2020年8月18日 21:50:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63470099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定