Java junit:未找到测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22469480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:56:06  来源:igfitidea点击:

junit: no tests found

javajunitintellij-idea

提问by JQuery Mobile

I have inherited a Java project and am new to Java development. I feel a good way for me to get comfortable with the code is to write some tests around it. I'm writing my code using IntelliJ.

我继承了一个 Java 项目,并且是 Java 开发的新手。我觉得让我适应代码的一个好方法是围绕它编写一些测试。我正在使用 IntelliJ 编写代码。

My existing project has a folder structure like this:

我现有的项目有这样的文件夹结构:

/myProject
  /src
    /main
      /java
        /com.lexcorp
          /core
            /email
              /providers
                emailProvider.java

I created a new project that will hold tests for this project. I would like this project to hold both unit and integration tests. Currently, my new project has a structure like this:

我创建了一个新项目,该项目将为此项目进行测试。我希望这个项目同时进行单元测试和集成测试。目前,我的新项目的结构如下:

/myProjectTests
  /src
    /main
      /java
        /com.lexcorp.core.email.providers
          emailProviderTest.java

The emailProviderTest.java file looks like the following:

emailProviderTest.java 文件如下所示:

package com.lexcorp.core.email.providers;

import junit.framework.TestCase;
import org.junit.Test;

public class EmailProviderTest extends TestCase {

    private final String username = "[testAccount]";

    private final String password = "[testPassword]";

    @Test
    public void thisAlwaysPasses() {
        assertTrue(true);
    }
}

This project has a Run/Debug configuration with the following properties:

该项目具有具有以下属性的运行/调试配置:

  • Test kind: All in package
  • Search for tests: In whole project
  • 测试种类:全包
  • 搜索测试:在整个项目中

When I run this configuration, I get an error that says:

当我运行此配置时,我收到一条错误消息:

junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access
defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
0(ParentRunner.java:53) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I do not understand why I'm getting an error that boils down to: "No tests found". While my project structures differ, the folder structures on the OS match (which is another thing that confuses me). Why am I getting this error and how do I fix it?

我不明白为什么我会收到一个错误,归结为:“未找到测试”。虽然我的项目结构不同,但操作系统上的文件夹结构匹配(这是另一件让我感到困惑的事情)。为什么我会收到此错误以及如何修复它?

采纳答案by BlackRainbow

I was getting error too

我也收到错误

junit.framework.AssertionFailedError: No tests found in ...

junit.framework.AssertionFailedError:在...中找不到测试

But I've forgot to specify

但我忘了指定

public class EmailProviderTest {

After sync projects it found tests. So maybe it helps someone else

同步项目后,它找到了测试。所以也许它可以帮助别人

回答by Reimeus

Extending junit.framework.TestCaseis the old JUnit 3 approach of implementing test cases which doesnt work as no methods start with the letters test. Since you're using JUnit 4, just declare the class as

扩展junit.framework.TestCase是旧的 JUnit 3 实现测试用例的方法,它不起作用,因为没有方法以字母test开头。由于您使用的是 JUnit 4,只需将该类声明为

@Test
public void method1(){
    // Do some stuff...
}

and the test method will be found from the @Testannotation.

并且会从@Test注释中找到测试方法。

Read: JUnit confusion: use 'extend Testcase' or '@Test'?

阅读:JUnit 混淆:使用“扩展测试用例”还是“@Test”?

回答by lidox

I was getting this too:

我也得到了这个:

junit.framework.AssertionFailedError: No tests found in ...

junit.framework.AssertionFailedError:在...中找不到测试

Solved by renaming the test method from method1

通过重命名从测试方法来解决方法1

@Test
public void testMethod1(){
    // Do some stuff...
}

to testMethod1

testMethod1

@DataProvider
public static Object[][] invalidAdjustment() {
    return new Object[][]{
            {"some \n text", false},
            };
}

回答by Pravin

The other reason generally i see people having method parameters, remove any parameters you might have in the test method, regardless you add @Test annotation , you need to have your method name starting "test" in order for Junit to pick you testcase. hope it helps.

另一个原因通常我看到人们有方法参数,删除测试方法中可能有的任何参数,无论您添加 @Test 注释,您都需要让方法名称以“test”开头,以便 Junit 选择您的测试用例。希望能帮助到你。

回答by Andrey Marchuk

I had it when using data provider and one of the parameters had a new line character, like this:

我在使用数据提供程序时使用了它,并且其中一个参数有一个换行符,如下所示:

@Test
public void getSomethingTest(){

Removing the \nsolved the issue

删除已\n解决的问题

回答by user1865456

if you are using jUnit 4 then not use extends TestCase, removing this fixed error.

如果您使用的是 jUnit 4,则不要使用extends TestCase,删除此固定错误。

回答by Fortran

Test method name public void testName() {}

测试方法名 public void test Name() {}

Where name()in source naming name methods.

源命名方法中的name()在哪里 。

回答by RobyB

I had the same issue, in Intellij IDEA, for Java framework. I was doing all right:

对于 Java 框架,我在 Intellij IDEA 中遇到了同样的问题。我做得很好:

  • the class inherited from TestCase
  • I've imported junit.framework.TestCase
  • I've add the decoration @Test
  • 从 TestCase 继承的类
  • 我已经导入了 junit.framework.TestCase
  • 我添加了装饰@Test

But I did one thing wrong: the name of the method didn't start with "test", in fact was:

但是我做错了一件事:方法的名称没有以 "test" 开头,实际上是:

@Test
public void testGetSomethingTest(){

and when I changed it into:

当我把它改成:

ant-version.jar
ant-junit-version.jar
ant-junit4-version.jar

I've resolved: the executor was finally able to recognize this method as a test method. I've changed nothing else.

我已经解决了:执行者终于能够将此方法识别为测试方法。 我没有改变其他任何东西。

回答by Lincoln

In my case, I needed junit4-version.jarin the classpath of the task junitand also:

就我而言,我需要junit4-version.jar在任务junit的类路径中,还需要:

apt-get install ant-optional

at the library of my ant installation (/usr/share/ant/lib).

在我的 ant 安装库中 ( /usr/share/ant/lib)。

I was getting the error "junit.framework.AssertionFailedError: No tests found in ..." while I hadn't had ant-junit4-*version*.jarin the right place.

我收到错误“ junit.framework.AssertionFailedError: No tests found in ...”而我没有ant-junit4-*version*.jar在正确的地方。

I corrected this by installing ant-optionaldebian/ubuntu package:

我通过安装ant-optionaldebian/ubuntu 包来纠正这个问题:

src/test/java/com/junit/test

回答by Ken Coppersmith

I was fretting with this and none of the answers helped me until i moved the test source file from this folder:

我对此很烦恼,直到我将测试源文件从此文件夹中移出之前,所有答案都没有帮助我:

src/test/java/com/junit

up to this folder:

直到这个文件夹:

##代码##