英文:
Running a localized Test with TestNG
问题
我的情况:
我正在使用TestNG运行一个测试,其中我调用一个第三方库的函数。这个库使用Locale.getDefault()进行初始化。
为了确保在运行测试的不同机器上获得相同的测试结果,我想设置特定的Locale,然后在测试结束后重置它。
不幸的是,尽管我可以将Locale设置为所需的值,但一些其他测试(不在同一个testClass中)似乎失败了...可能是由于格式化问题(本地化)。
private Locale locale;
@BeforeClass
public void init() {
locale = Locale.getDefault();
Locale.setDefault(Locale.US);
}
@AfterClass
public void teardown() {
Locale.setDefault(locale);
}
@Test(dataProvider = "someDataProvider")
public void testThisWithUSLocale(String element) throws Exception {
String resultToTest = myClass.createSomething(element);
Assert.assertFalse(resultToTest.isEmpty());
}
我尝试在@BeforeMethod中设置Locale,但遇到了相同的问题...
测试是否异步运行?在这个测试中设置Locale是否真的会影响其他测试?
英文:
I'm stuck here:
My scenario:
I'm running a test with TestNG where I call a function of a 3rd party library. This library gets initialized with Locale.getDefault().
To get the same test result independently from the machine the test is run on, I want to set a specific Locale and reset it afterwards.
Unfortunately, it seems that though I can set the Locale to my desired value, some other tests (not in the same testClass) fail... probably due to formatting issues (localization).
private Locale locale;
@BeforeClass
public void init() {
locale = Locale.getDefault();
Locale.setDefault(Locale.US);
}
@AfterClass
public void teardown() {
Locale.setDefault(locale);
}
@Test(dataProvider = "someDataProvider")
public void testThisWithUSLocale(String element) throws Exception {
String resultToTest = myClass.createSomething(element);
Assert.assertFalse(resultToTest.isEmpty());
}
I tried setting Locale in @BeforeMethod but had the same issue...
Are tests run async? does setting the Locale in this test really affect the other tests?
答案1
得分: 1
更改JVM运行时的区域设置是一个棘手的过程,整个应用程序都需要为此做好准备。从setDefault
的javadoc中可以看到:
由于更改默认区域设置可能会影响许多不同功能领域,只有在调用者准备好重新初始化在同一个Java虚拟机内运行的区域设置敏感代码时,才应该使用此方法。
您的测试设置看起来正确,但无论您的代码(或第三方库)做什么,可能都不支持更改区域设置。
英文:
Changing the locale while the JVM is running is a tricky process, and the whole application needs to be prepared for it. From the javadocs of setDefault
> Since changing the default locale may affect many different areas of functionality, this method should only be used if the caller is prepared to reinitialize locale-sensitive code running within the same Java Virtual Machine.
Your test setup looks correct, but whatever your code (or the 3rd party library) do probably doesn't support a change of the Locale.
答案2
得分: 1
好的,我找到了问题:
初始时为FORMAT设置了Locale。因此,通过在@BeforeClass
中运行所有内容,我的测试正常工作。但是在用先前保存的(但错误的)方式Locale.setDefault(locale);
“重置”了一切后,我正在设置所有类别,而不仅仅是FORMAT,从而使其他测试失败。正确的做法是:
private Locale locale;
@BeforeClass
public void init() {
locale = Locale.getDefault(Locale.Category.FORMAT);
Locale.setDefault(Locale.Category.FORMAT, Locale.US);
}
@AfterClass
public void teardown() {
Locale.setDefault(locale);
}
英文:
Alright, I found the problem:
Locale was initially set for FORMAT. So, by running everything in @BeforeClass
, my tests worked fine, but after "resetting" everything with Locale.setDefault(locale);
that I saved earlier (but in a wrong way), I'm setting ALL Categories, and not just FORMAT making the other test fail. Correct is:
private Locale locale;
@BeforeClass
public void init() {
locale = Locale.getDefault(Locale.Category.FORMAT);
Locale.setDefault(Locale.Category.FORMAT, Locale.US);
}
@AfterClass
public void teardown() {
Locale.setDefault(locale);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论