简体   繁体   English

为什么我的 JUnit 测试没有在 Eclipse 运行?

[英]Why is my JUnit test not running in Eclipse?

It's been 3 hours now and I still didn't find a solution, even though I seem to have read all related questions already.现在已经 3 个小时了,我仍然没有找到解决方案,尽管我似乎已经阅读了所有相关问题。

I am building an Android application and I just want to create a couple of simple Unit Tests that test my basic functions.我正在构建一个 Android 应用程序,我只想创建几个简单的单元测试来测试我的基本功能。 I don't need to test any Android related logic or activity features.我不需要测试任何 Android 相关的逻辑或活动功能。

So I have created a new directory in my solution in which I have created a new JUnit Test Case .所以我在我的解决方案中创建了一个新目录,我在其中创建了一个新的JUnit 测试用例

To keep things simple my test methods are not testing much yet, but even when doing a Right Click > Run As > JUnit Test , it's not doing anything.为了简单起见,我的测试方法还没有进行太多测试,但即使在执行Right Click > Run As > JUnit Test时,它也没有做任何事情。

As you can see in my screenshot the JUnit pane on the left shows my test is terminated but does not show any test that has been executed.正如您在我的屏幕截图中所见,左侧的 JUnit 窗格显示我的测试已终止,但未显示任何已执行的测试。

I have created a simple Unit Test in a new Java Project and then it's working.我在新的Java 项目中创建了一个简单的单元测试,然后它开始工作了。 If I repeat the same steps in a new Android Application Project it's not working.如果我在新的Android 应用程序项目中重复相同的步骤,它就无法正常工作。

What do I need to do to run my simple Unit Tests?!我需要做什么来运行我的简单单元测试?!

Thanks!谢谢!

我的非运行单元测试的屏幕截图

(My Compiler Compliance Level is 1.6) (我的编译器合规级别是 1.6)

You either don't have JUnit on the build path or you don't have the library (jar) at hand.您要么在构建路径上没有 JUnit,要么手头没有库 (jar)。 Make sure both are in place.确保两者都到位。

I think you will need an unit test suite if all else fails:我认为如果所有其他方法都失败了,您将需要一个单元测试套件:

@RunWith(Suite.class)
@SuiteClasses({ GpsLocationTest.class })
public class AllTests {

} 

There is one more thing you can do: check whether you import the right @Test annotation.您还可以做一件事:检查是否导入了正确的@Test注释。 Restart Eclipse and clean your project if the problem persists.如果问题仍然存在,请重新启动 Eclipse 并清理您的项目。

You may want to refer to the vogella guide about unit testing.您可能需要参考有关单元测试的vogella 指南

您可以使用 AndroidTestCase,它继承自 junit.framework.TestCase,而不是 org.junit.Test。

This is related to Memory Issue.这与内存问题有关。 Simply add these line in VM argument: right click on Junit Test -> Run as -> Run Configuration -> Arguments -> add "-XX:MaxPermSize=512m" under VM argument只需在 VM 参数中添加这些行:右键单击 Junit Test -> Run as -> Run Configuration -> Arguments -> 在 VM 参数下添加“-XX:MaxPermSize=512m”

Go to Window -> Show View -> Error Log to see what the actual error is.转到Window -> Show View -> Error Log以查看实际错误是什么。
For my case it was No test found with test runner 'Junit 5'.对于我的情况,测试运行程序“ Junit 5”没有找到测试。
Then one can google for respective solution.然后可以谷歌搜索相应的解决方案。

The java Build path of the project has Junit4.jar lets say and the Run configuration for the test you are running has Junit5 - then it causes to terminate and nothing happens. java 项目的构建路径有 Junit4.jar 可以说,您正在运行的测试的运行配置有 Junit5 - 然后它会导致终止并且没有任何反应。

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

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