简体   繁体   English

仅在测试文件夹中运行测试

[英]Run tests only in test folder

I have a test project in java, that runs all tests found in the projects listed as dependencies for this project. 我在Java中有一个测试项目,该项目运行在项目中找到的所有测试,并列为该项目的依赖项。

@RunWith(ClasspathSuite.class)
@ClassnameFilters({ "com.example.*_Test" })
public class AllRegularTests {
   // nothing to do
}

My tests are usually in the test/ folder not in the src/ 我的测试通常在test/文件夹中,而不在src/

How can I make the test suite look only for classes in test/ , and not in src/ ? 如何使测试套件仅查找test/类,而不查找src/

Maybe you can do it with the testSourceDirectory definition but I'm not sure. 也许您可以使用testSourceDirectory定义来做到这一点,但我不确定。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
                <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

Few things seems to be wrong here : 这里似乎没有什么错:

  1. By default Maven uses the following naming conventions when looking for tests to run: Test* *Test *TestCase Your test class doesn't follow these conventions. 默认情况下,Maven在寻找要运行的测试时使用以下命名约定:Test * * Test * TestCase您的测试类不遵循这些约定。 You should rename it or configure Maven Surefire Plugin to use another pattern for test classes. 您应该重命名它或将Maven Surefire插件配置为对测试类使用其他模式。
  2. unit test code should put under the test folder, it can not be recognized as test class if you put it under the main folder. 单元测试代码应放在test文件夹下,如果将其放在主文件夹下则不能识别为测试类。 eg. 例如。

Wrong /my_program/src/main/java/NotTest.java /my_program/src/main/java/NotTest.java 错误

Right /my_program/src/test/java/MyTest.java 正确的 /my_program/src/test/java/MyTest.java

Also, check if your test classes directory (eg src/test/java) corresponds to directory listed in property in your pom.xml under property. 另外,检查您的测试类目录(例如src / test / java)是否对应于pom.xml中property属性下列出的目录。 Took me a while to find that. 花了我一段时间才能找到那个。

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

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