简体   繁体   English

“Maven Surefire 插件”在测试您的构建时意味着什么?

[英]What does the "Maven Surefire plugin" means when it test your build?

I am working on understanding Maven and I'm learning about building your Java app with it.我正在努力了解 Maven,并且正在学习如何使用它构建您的 Java 应用程序。 So when I do a :所以当我做一个:

maven package

It does build my jar as expected but I see in the output console that Maven does build tests (it always say that the test a run and there are no failure).它确实按预期构建了我的 jar,但我在输出控制台中看到 Maven 确实构建了测试(它总是说测试运行并且没有失败)。 I researched on the web about that and learned that Maven use a plugin called Maven Surefire.我在网上对此进行了研究,并了解到 Maven 使用了一个名为 Maven Surefire 的插件。 But I can't understand what does that plugin do to my code, what does the tests "means" ?但我不明白该插件对我的代码做了什么,测试“意味着”什么? What does the tests do with my code and how it works behind the console ?测试对我的代码有什么作用以及它如何在控制台后面工作?

The Maven surefire plugin runs the tests you have written. Maven surefire 插件运行您编写的测试。 These are usually in the src/test/java folder.这些通常位于src/test/java文件夹中。 If you have none, the plugin does nothing.如果你没有,插件什么也不做。

Is this only one question?这只是一个问题吗? :D :D

So.所以。 Different things are going on.不同的事情正在发生。

You create an application with Java.您使用 Java 创建一个应用程序。 To test the single components / packages / classes that you create most people use JUnit or TestNg .为了测试您创建的单个组件/包/类,大多数人使用JUnitTestNg You usually have dedicated test classes that verify your production code behaves as intended without you clicking through all the things on every change.您通常有专门的测试类来验证您的生产代码是否按预期运行,而无需在每次更改时点击所有内容。

When you now use maven to run your build the pom.xml file defines a packaging - in your case "jar" since you create a jar file.当您现在使用 maven 运行您的构建时, pom.xml 文件定义了一个包装 - 在您的情况下为“jar”,因为您创建了一个 jar 文件。 The packaging defines what set of default plugins run in the defined maven phases .打包定义定义的 maven 阶段中运行的默认插件集 You probably recognize package here.您可能在这里认识package Maven executes all phases up to package and the registered / configured plugins. Maven 执行所有阶段直到打包和注册/配置的插件。

To execute those tests maven provides the surefire plugin which supports running JUnit or TestNg tests.为了执行这些测试,maven 提供了支持运行 JUnit 或 TestNg 测试的 surefire插件 If you follow the directory conventions your tests reside in src/test/java and the surefire includes naming convention maven will execute those tests in every build (as this is the best practice).如果您遵循目录约定,您的测试驻留在src/test/java ,并且肯定包含命名约定maven 将在每个构建中执行这些测试(因为这是最佳实践)。 If you also want to write integration tests then there is the failsafe plugin .如果您还想编写集成测试,则可以使用故障安全插件 That plugin is not enabled by default and runs in different maven phases.该插件默认未启用,并在不同的 maven 阶段运行。

So the tests just run your production code - in fact they just do what you implement in the tests.所以测试只是运行你的生产代码——事实上,它们只是做你在测试中实现的。 They don't alter it in any way.他们不会以任何方式改变它。

The maven introduction documentation has step by step explanations: Maven in 5 Minutes and the Getting Started Guide . maven 介绍文档有分步说明: Maven in 5 MinutesGetting Started Guide

Starting from scratch this is probably a lot.从头开始,这可能很多。 So don't rush this.所以不要着急。 The build setup and test setup are very important things to have.构建设置和测试设置是非常重要的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM