如何在Maven中配置时区,因此Pitest会选择它

发布于 2025-02-06 20:46:08 字数 2180 浏览 1 评论 0原文

我尝试将pitest应用于依赖时区设置的较旧项目。 某些测试专门用于节省日光的问题,而一年中的某些日子在当地时代或不到24小时。 我无法更改代码或测试。

这些测试失败了,但否则可以运行良好。

从我看到的内容来看,Junit/surefire考虑了Maven POM中设置的全局属性,但Pitest却没有。

    <properties>
        <argLine>-Duser.timezone=Europe/Berlin</argLine>
    <properties>

另外,如果将命令行直接设置为MVN验证-duser.timezone =欧洲/柏林...它不起作用。

对我来说可能的解决方法是:

  • 排除受影响的测试类
  • tz =欧洲/柏林/柏林在系统级别

我很乐意了解其他可能性,以以一种pittest选择它的方式设置时区 为

,对于完整性,这是我对Maven配置文件的第一种方法,因此pitests可以在MVN验证-P突变测试 上运行:

        <profile>
            <id>mutation-testing</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.pitest</groupId>
                        <artifactId>pitest-maven</artifactId>
                        <version>1.8.0</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>mutationCoverage</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <timestampedReports>false</timestampedReports>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.pitest</groupId>
                                <artifactId>pitest-junit5-plugin</artifactId>
                                <version>0.16</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>

也许使用jvmargs pitest的配置?如何?

请注意,我是Pitest和突变测试的新手,所以也许我做一些愚蠢的事情。

I try to apply Pitest to an older project that relies on timezone settings.
Some tests are specifically for daylight-saving issues, when certain days in a year have more or less than 24 hours in local time.
I cannot change the code or the tests.

These tests fail with Pitest but run fine otherwise.

From what I see, JUnit/Surefire takes into account the global property user.timezone that is set in Maven POM, but Pitest does not.

    <properties>
        <argLine>-Duser.timezone=Europe/Berlin</argLine>
    <properties>

Also if set on command line directly as mvn verify -Duser.timezone=Europe/Berlin ... it does not work.

Possible workarounds for me are:

  • Exclude the affected test classes
  • Set TZ=Europe/Berlin on system level

I would be happy to learn about other possibilities to set the timezone in a way that Pitest picks it up.

For completeness, this is my first approach to a Maven profile so that Pitests runs on mvn verify -P mutation-testing:

        <profile>
            <id>mutation-testing</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.pitest</groupId>
                        <artifactId>pitest-maven</artifactId>
                        <version>1.8.0</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>mutationCoverage</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <timestampedReports>false</timestampedReports>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.pitest</groupId>
                                <artifactId>pitest-junit5-plugin</artifactId>
                                <version>0.16</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>

Maybe it is possible with the jvmArgs configuration of Pitest? How?

Note that I am new to Pitest and mutation testing so maybe I do something stupid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2025-02-13 20:46:08

为了插入突变体,使它们彼此隔离并处理突变可能引起的无限循环,Pitest必须在子过程中运行代码。

配置可以使用jvmargs参数传递给他们。

<jvmArgs><value>-Duser.timezone=Europe/Berlin</value></jvmArgs>

将在Pitest启动的所有JVM中设置时区。

顺便说一句,如果您的代码基于时间和日期做出决定,通常是一个好主意,将其作为可以注入的代码的显式依赖性(例如,使用java.time.clock) 。这样,测试可以用细粒度来操纵时间,而无需依靠全球设置。

In order to insert mutants, keep them isolated from each other and handle the infinite loops that mutations might cause, pitest has to run code in child processes.

Configutation can be passed to them using the jvmArgs parameter.

<jvmArgs><value>-Duser.timezone=Europe/Berlin</value></jvmArgs>

Will set the timezone in all jvms that pitest launches.

As an aside, if your code makes decisions based on time and date it is usually a good idea to make this an explicit dependency of the code that can be inject (eg with a java.time.Clock). This way tests can manipulate time in a fine grained manner, and do not need to rely on global settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文