跳过在Maven验证阶段的特定测试类。

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

Skip a specific test class during maven verify phase

问题

我有几个用@RunWith@ContextConfiguration注释的测试类。
我在我的Maven SpringBoot项目中使用以下依赖项:

<dependency>
  <groupId>org.springframework.batch</groupId>
  <artifactId>spring-batch-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

当我运行mvn clean verify时,它会执行所有我的测试类。

现在我有一个特定的测试类,我想在verify阶段忽略它。

我尝试使用@ConditionalOnProperty对其进行注释,所以最后这个类如下:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { RestTemplate.class, AvailabilityService.class, VerifyService.class, ConfirmService.class })
@ComponentScan(basePackages = "it.alpitour")
@TestPropertySource("classpath:application-test.properties")
@ConditionalOnProperty("test.api.real.run")
public class ToIgnoreTest {

所以我在我的application-test.properties文件中添加了一个属性:

test.api.real.run=false

但是测试仍然被执行而没有被忽略,在verify阶段。

我该如何跳过它?

谢谢

编辑

我更喜欢不使用@Ignore,因为我需要在某个时候手动运行那个特定的测试。
我所需要的只是在运行所有其他测试时排除它。

英文:

I have several test classes annotated with @RunWith and @ContextConfiguration.
I use the following dependencies in my maven SpringBoot project:

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.batch&lt;/groupId&gt;
  &lt;artifactId&gt;spring-batch-test&lt;/artifactId&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;junit&lt;/groupId&gt;
  &lt;artifactId&gt;junit&lt;/artifactId&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

and when I run mvn clean verify it executes all my test classes.

Now I have a specific test class that I want to ignore during the verify phase.

I tried to annotate it with @ConditionalOnProperty, so at the end the class is as following:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { RestTemplate.class, AvailabilityService.class, VerifyService.class,ConfirmService.class })
@ComponentScan(basePackages = &quot;it.alpitour&quot;)
@TestPropertySource(&quot;classpath:application-test.properties&quot;)
@ConditionalOnProperty(&quot;test.api.real.run&quot;)
public class ToIgnoreTest {

so I have added a property inside my application-test.properties file:

test.api.real.run=false

but the test is still executed and not ignored, during the verify phase.

How can I skip it?

Thanks

edit:

I prefer not to use @Ignore because I need at some point to run that specific test manually.
What I just need is to exclude that one when all the other tests are running.

答案1

得分: 1

你可以在你的测试类上使用注解@ignore,如果你使用的是 Junit 4,否则你可以使用以下命令:

mvn -Dtest=\!YourTest* install

但我认为,由于你使用的是 Spring Boot 而不是 Spring,更好的答案应该是:https://stackoverflow.com/questions/39048171/how-to-run-turn-off-selective-tests-based-on-profiles-in-spring-boot

英文:

You can use the annotation @ignore on your test class if you are using Junit 4 or else you could use the command as follows :

mvn -Dtest=\!YourTest* install

but I think since you are using springboot and not spring the better answer would be : https://stackoverflow.com/questions/39048171/how-to-run-turn-off-selective-tests-based-on-profiles-in-spring-boot

huangapple
  • 本文由 发表于 2020年8月13日 20:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63395259.html
匿名

发表评论

匿名网友

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

确定