简体   繁体   English

与Java项目一起运行JUnit测试

[英]Running JUnit tests together with Java project

I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time. 我正在Intellij中编写代码,并在项目中包含了JUnit测试类,并且我了解JUnit的运行应始终在构建时完成。

Is there a way to first run the JUnit and only if there were no test error run the project itself ? 有没有一种方法可以首先运行JUnit,并且仅当没有测试错误时才运行项目本身? I want them to run together with 1 click (NOT run them seperately/manually). 我希望它们一键运行(不要单独/手动运行它们)。

Also, i would like the above to work even when the project is packed as a .jar file. 另外,即使项目打包为.jar文件,我也希望以上内容能够正常工作。

How can it be done ? 如何做呢 ?

Thanks ! 谢谢 !

In Intellij: 在Intellij中:

Run -> Edit Configurations 运行->编辑配置

  1. Create a JUnit configuration for your tests 为测试创建一个JUnit配置

  2. Create a Run configuration for your project. 为您的项目创建一个运行配置。 And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1. It doesn't matter how your project is packed (jar, ...) 然后在“启动前”上:添加->运行另一种配置,然后选择在点1创建的配置。打包项目的方式无关紧要(jar,...)

Normally this is done by using a build management tool like: maven, gradle, ant. 通常,这是通过使用诸如maven,gradle,ant之类的构建管理工具来完成的。 In this way the build tool will run tests for you and stop if they fail. 这样,构建工具将为您运行测试,并在测试失败时停止。

With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code. 使用maven,它只是一个命令: mvn clean package exec:java ,它将编译代码,构建项目,运行测试并执行您的代码。

See example project: https://github.com/HaveACupOfJava/maven-jar-demo 查看示例项目: https : //github.com/HaveACupOfJava/maven-jar-demo

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

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