JUnit:如何在test utils类中避免“没有runnable方法”

时间:2022-02-08 05:07:30

I have switched to JUnit4.4 from JUnit3.8. I run my tests using ant, all my tests run successfully but test utility classes fail with "No runnable methods" error. The pattern I am using is to include all classes with name *Test* under test folder.

我已经从JUnit3.8切换到JUnit4.4。我使用ant运行我的测试,我的所有测试都成功运行,但测试实用程序类失败并显示“No runnable methods”错误。我使用的模式是在测试文件夹下包含名称为* Test *的所有类。

I understand that the runner can't find any method annotated with @Test attribute. But they don't contain such annotation because these classes are not tests. Surprisingly when running these tests in eclipse, it doesn't complain about these classes.

据我所知,跑步者找不到任何用@Test属性注释的方法。但是它们不包含这样的注释,因为这些类不是测试。令人惊讶的是,当在eclipse中运行这些测试时,它并没有抱怨这些类。

In JUnit3.8 it wasn't a problem at all since these utility classes didn't extend TestCase so the runner didn't try to execute them.

在JUnit3.8中,它根本不是问题,因为这些实用程序类没有扩展TestCase,所以跑步者没有尝试执行它们。

I know I can exclude these specific classes in the junit target in ant script. But I don't want to change the build file upon every new utility class I add. I can also rename the classes (but giving good names to classes was always my weakest talent :-) )

我知道我可以在ant脚本中的junit目标中排除这些特定的类。但我不想在我添加的每个新实用程序类中更改构建文件。我也可以重命名课程(但是给课程好名字总是我最弱的天才:-))

Is there any elegant solution for this problem?

这个问题有没有优雅的解决方案?

10 个解决方案

#1


43  

Assuming you're in control of the pattern used to find test classes, I'd suggest changing it to match *Test rather than *Test*. That way TestHelper won't get matched, but FooTest will.

假设您已经控制了用于查找测试类的模式,我建议将其更改为匹配* Test而不是* Test *。这样,TestHelper将无法匹配,但FooTest会。

#2


131  

Annotate your util classes with @Ignore. This will cause JUnit not to try and run them as tests.

使用@Ignore注释您的util类。这将导致JUnit不尝试将它们作为测试运行。

#3


73  

My specific case has the following scenario. Our tests

我的具体情况有以下情况。我们的测试

public class VenueResourceContainerTest extends BaseTixContainerTest

all extend

一切都延伸

BaseTixContainerTest

and JUnit was trying to run BaseTixContainerTest. Poor BaseTixContainerTest was just trying to setup the container, setup the client, order some pizza and relax... man.

和JUnit试图运行BaseTixContainerTest。可怜的BaseTixContainerTest只是试图设置容器,设置客户端,订购一些披萨并放松......男人。

As mentioned previously, you can annotate the class with

如前所述,您可以使用注释对类进行注释

@Ignore

But that caused JUnit to report that test as skipped (as opposed to completely ignored).

但是这导致JUnit报告该测试被跳过(而不是完全被忽略)。

Tests run: 4, Failures: 0, Errors: 0, Skipped: 1

That kind of irritated me.

那种烦恼我。

So I made BaseTixContainerTest abstract, and now JUnit truly ignores it.

所以我创建了BaseTixContainerTest抽象,现在JUnit真的忽略了它。

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

#4


32  

To prevent JUnit from instantiating your test base class just make it

为了防止JUnit实例化你的测试基类,只需要它

public abstract class MyTestBaseClass { ... whatever... }

(@Ignore reports it as ignored which I reserve for temporarily ignored tests.)

(@Ignore报告它被忽略,我保留暂时忽略的测试。)

#5


13  

  1. If this is your base test class for example AbstractTest and all your tests extends this then define this class as abstract
  2. 如果这是您的基本测试类,例如AbstractTest,并且您的所有测试都扩展了它,那么将此类定义为abstract
  3. If it is Util class then better remove *Test from the class rename it is MyTestUtil or Utils etc.
  4. 如果它是Util类,那么最好从类中删除* Test重命名它是MyTestUtil或Utils等。

#6


5  

Ant now comes with the skipNonTests attribute which was designed to do exactly what you seem to be looking for. No need to change your base classes to abstract or add annotations to them.

Ant现在附带了skipNonTests属性,该属性旨在完全按照您的要求进行操作。无需更改基类以抽象或向其添加注释。

#7


5  

Be careful when using an IDE's code-completion to add the import for @Test.

使用IDE的代码完成时要小心,为@Test添加导入。

It has to be import org.junit.Test and not import org.testng.annotations.Test, for example. If you do the latter, you'll get the "no runnable methods" error.

它必须是import org.junit.Test而不是导入org.testng.annotations.Test,例如。如果你执行后者,你将得到“无运行方法”错误。

#8


4  

What about adding an empty test method to these classes?

如何为这些类添加空测试方法呢?

public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
    assertTrue(true); // do nothing;
}

#9


0  

I was also facing a similar issue ("no runnable methods..") on running the simplest of simple piece of code (Using @Test, @Before etc.) and found the solution nowhere. I was using Junit4 and Eclipse SDK version 4.1.2. Resolved my problem by using the latest Eclipse SDK 4.2.2. I hope this helps people who are struggling with a somewhat similar issue.

在运行最简单的一段代码(使用@Test,@ Before等)时,我也遇到了类似的问题(“没有可运行的方法......”),并且找不到解决方案。我使用的是Junit4和Eclipse SDK 4.1.2版。通过使用最新的Eclipse SDK 4.2.2解决了我的问题。我希望这可以帮助那些在类似问题上苦苦挣扎的人们。

#10


-1  

In your test class if wrote import org.junit.jupiter.api.Test; delete it and write import org.junit.Test; In this case it worked me as well.

在你的测试类中如果写入import org.junit.jupiter.api.Test;删除它并写入import org.junit.Test;在这种情况下,它也是我的工作。

#1


43  

Assuming you're in control of the pattern used to find test classes, I'd suggest changing it to match *Test rather than *Test*. That way TestHelper won't get matched, but FooTest will.

假设您已经控制了用于查找测试类的模式,我建议将其更改为匹配* Test而不是* Test *。这样,TestHelper将无法匹配,但FooTest会。

#2


131  

Annotate your util classes with @Ignore. This will cause JUnit not to try and run them as tests.

使用@Ignore注释您的util类。这将导致JUnit不尝试将它们作为测试运行。

#3


73  

My specific case has the following scenario. Our tests

我的具体情况有以下情况。我们的测试

public class VenueResourceContainerTest extends BaseTixContainerTest

all extend

一切都延伸

BaseTixContainerTest

and JUnit was trying to run BaseTixContainerTest. Poor BaseTixContainerTest was just trying to setup the container, setup the client, order some pizza and relax... man.

和JUnit试图运行BaseTixContainerTest。可怜的BaseTixContainerTest只是试图设置容器,设置客户端,订购一些披萨并放松......男人。

As mentioned previously, you can annotate the class with

如前所述,您可以使用注释对类进行注释

@Ignore

But that caused JUnit to report that test as skipped (as opposed to completely ignored).

但是这导致JUnit报告该测试被跳过(而不是完全被忽略)。

Tests run: 4, Failures: 0, Errors: 0, Skipped: 1

That kind of irritated me.

那种烦恼我。

So I made BaseTixContainerTest abstract, and now JUnit truly ignores it.

所以我创建了BaseTixContainerTest抽象,现在JUnit真的忽略了它。

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

#4


32  

To prevent JUnit from instantiating your test base class just make it

为了防止JUnit实例化你的测试基类,只需要它

public abstract class MyTestBaseClass { ... whatever... }

(@Ignore reports it as ignored which I reserve for temporarily ignored tests.)

(@Ignore报告它被忽略,我保留暂时忽略的测试。)

#5


13  

  1. If this is your base test class for example AbstractTest and all your tests extends this then define this class as abstract
  2. 如果这是您的基本测试类,例如AbstractTest,并且您的所有测试都扩展了它,那么将此类定义为abstract
  3. If it is Util class then better remove *Test from the class rename it is MyTestUtil or Utils etc.
  4. 如果它是Util类,那么最好从类中删除* Test重命名它是MyTestUtil或Utils等。

#6


5  

Ant now comes with the skipNonTests attribute which was designed to do exactly what you seem to be looking for. No need to change your base classes to abstract or add annotations to them.

Ant现在附带了skipNonTests属性,该属性旨在完全按照您的要求进行操作。无需更改基类以抽象或向其添加注释。

#7


5  

Be careful when using an IDE's code-completion to add the import for @Test.

使用IDE的代码完成时要小心,为@Test添加导入。

It has to be import org.junit.Test and not import org.testng.annotations.Test, for example. If you do the latter, you'll get the "no runnable methods" error.

它必须是import org.junit.Test而不是导入org.testng.annotations.Test,例如。如果你执行后者,你将得到“无运行方法”错误。

#8


4  

What about adding an empty test method to these classes?

如何为这些类添加空测试方法呢?

public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
    assertTrue(true); // do nothing;
}

#9


0  

I was also facing a similar issue ("no runnable methods..") on running the simplest of simple piece of code (Using @Test, @Before etc.) and found the solution nowhere. I was using Junit4 and Eclipse SDK version 4.1.2. Resolved my problem by using the latest Eclipse SDK 4.2.2. I hope this helps people who are struggling with a somewhat similar issue.

在运行最简单的一段代码(使用@Test,@ Before等)时,我也遇到了类似的问题(“没有可运行的方法......”),并且找不到解决方案。我使用的是Junit4和Eclipse SDK 4.1.2版。通过使用最新的Eclipse SDK 4.2.2解决了我的问题。我希望这可以帮助那些在类似问题上苦苦挣扎的人们。

#10


-1  

In your test class if wrote import org.junit.jupiter.api.Test; delete it and write import org.junit.Test; In this case it worked me as well.

在你的测试类中如果写入import org.junit.jupiter.api.Test;删除它并写入import org.junit.Test;在这种情况下,它也是我的工作。