单元测试类在Eclipse中运行旧版本 [英] Unit test class runs old version in eclipse

查看:74
本文介绍了单元测试类在Eclipse中运行旧版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个JUnit测试类,该类会逐渐更新-我每隔几周添加一次测试,有时还会修改测试的代码.

I have a JUnit test class in my project which is updated incrementally - I add tests every few weeks and sometimes modify the tests' code.

令人惊讶的是,当我使用Eclipse JUnit Runner 4运行测试类时,它将运行旧代码(在更新之前)而不是新代码.我可以更改代码,添加或删除测试,但它仍然可以运行旧代码.

Surprisingly, when I run the test class using eclipse JUnit Runner 4, it runs my old code (before the update) and not the new one. I can change the code, add or remove tests but it still runs the old code.

我试图找出问题所在,并运行一个我刚刚编写的测试,却得到了臭名昭著的无根测试"错误,而没有任何堆栈跟踪信息,可以让我知道问题出在哪里.

I tried to isolate the problem and run a single test that I've just written and got the infamous "Unrooted tests" error without any stack trace to give me a clue what the problem is.

我做了一些研究,并且根据这里的其他一些线程,很多人都遇到了有关JUnit 3 \ JUnit 4兼容性的问题,但是这里不是这种情况-我用@Test注释了我的所有测试,不要扩展TestCase类.

I did some research and according to a few other threads here, many people encountered this problem with regards to JUnit 3 \ JUnit 4 compatibility, but this is not the case here - I annotate all my tests with @Test and I do NOT extends the TestCase class.

清洁/构建所有Eclipse项目无济于事. 但是,当我mvn clean install我的项目时,此问题确实可以解决,但这会花费太多时间. 另外,重命名该类(eclipse中的Alt + Shift + R)可以使新代码立即运行,但是将其重命名为其原始(有效)名称,可以使旧代码再次运行(WTF?)

Cleaning / Building all eclipse projects doesn't help. However, this problem does work itself around when I mvn clean install my project, but this takes too much time. Also, renaming the class (Alt+Shift+R in eclipse) gets the new code to run immediately, but renaming it back to its original (and valid) name, gets the old code to run again (WTF?)

我们将不胜感激, 10倍

Help would be appreciated, 10x

推荐答案

我设法自己解决了这个问题(受到Harlard的评论的启发). 在检查了项目的目标目录后,我注意到其中的test-classes目录不包含测试的二进制文件. 然后,我注意到我将类放错了src/test/java中,并放置在与我的项目的包结构不符的包结构中,它们是src/test/java的直接子目录. 因此,eclipse不能将它们放置在正确的位置,并且为它们生成二进制文件的唯一方法是执行maven构建. 将我所有的测试类重构为正确的包结构后,一切运行正常.

I managed to solve the problem on my own (inspired by a comment contributed by Harlard). After examining the target directory of my project, I noticed that the test-classes dir inside it doesn't include the binaries of my test. I then noticed that I misplaced the classes within src/test/java and put them in a package structure that doesn't conform to the package structure of my project, they were direct sub-directories of src/test/java. Therefore, eclipse didn't put them in the correct place and the only way to generate binaries for them was by executing a maven build. After refactoring all my test classes to the correct package structure, everything worked perfectly.

这篇关于单元测试类在Eclipse中运行旧版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆