是否可以将 JUnit 测试用例与 Eclipe 中测试的类同步?

发布于 2024-07-27 23:07:34 字数 184 浏览 6 评论 0原文

我知道可以使用 JUnit 中的向导创建 TestCase 或 TestSuite,但是在测试下的类被修改后(例如方法签名修改或新添加的方法),如何同步代码。 我希望我的测试用例能够在我的测试用例中同步(删除或添加)那些更改的方法/参数/方法签名。

我尝试在Google上搜索没有结果,也许有一个eclipse插件可以实现这一点?

I know it is possible to create a TestCase or TestSuite with the wizard within JUnit, but how does one sync the code after the class under the test has been modified, such as method signature modification or newly added methods. I would like my TestCases to be able to sync up (remove or add) those changed methods/parameters/method signatures within my TestCases.

I tried searching on Google to no avail, maybe there is an eclipse plugin for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

別甾虛僞 2024-08-03 23:07:34

正如前面提到的,只要您使用 Eclipse 工具进行重构,而不是通过手动重命名等方式进行重构,重命名或移动等重构就会自动反映在测试用例中。

对于新方法,不可能自动生成测试。 当然,自动生成的代码有一些例外,您可以在其中控制生成,也可以生成测试用例,但对于“正常”手动代码,您可以做的最好的事情就是提供存根(空方法),这有什么用在那里面?

更好的方法是使用诸如 Cobertura艾玛 ,它恰好有一个不错的 Eclipse 插件,它允许您在源代码中查看代码是什么被测试覆盖,哪些没有。 这就是您需要更多测试的地方的报告。

As has been mentioned before, refactorings such as renames or moves will be automatically reflected in the test cases as long as you refactor using the Eclipse tooling and not by manually renaming for example.

As for new methods, it's impossible to generate tests automatically. Of course there are some exceptions for auto-generated code where you control the generation and where you could generate test cases as well but for "normal" manual code the best you could do was to provide stubs (empty methods), and what's the use in that?

A better approach is to track code coverage using a tool such as Cobertura or Emma, which happens to have a nice Eclipse plugin that allows you to see, inside your source code, what code is being covered by tests and which isn't. This then is your report of where you need more testing.

您的好友蓝忘机已上羡 2024-08-03 23:07:34

与生产代码的结构具有 1-1 关系的测试是测试气味。 最好将测试编写为系统行为的规范(~每个行为一个测试),而不是根据生产代码生成测试(~每个方法一个测试)。

引用自 http://blog.daveastels.com/files/BDD_Intro.pdf

当您意识到这都是关于指定行为而不是编写测试时,您的观点是
观点转变。 突然间,为每个生产类设置一个测试类的想法是可笑的
限制。 以及用自己的测试方法来测试每个方法的想法(在一个
1-1关系)会很可笑。

Tests which have a 1-1 relationship with the structure of the production code are a test smell. It's much better for the tests to be written as a specification of the system's behaviour (~one test per behaviour), instead of having tests generated based on the production code (~one test per method).

Quoted from http://blog.daveastels.com/files/BDD_Intro.pdf

When you realize that it's all about specifying behaviour and not writing tests, your point of
view shifts. Suddenly the idea of having a Test class for each of your production classes is ridiculously
limiting. And the thought of testing each of your methods with its own test method (in a
1-1 relationship) will be laughable.

淡淡的优雅 2024-08-03 23:07:34

如果您使用自动重构更改方法签名,则测试用例以及调用该方法的所有其他代码将自动更新。

对于新添加的方法,我不知道如何自动更新测试类。

If you change method signatures using the automated refactoring, then the test cases - and all other code that calls the method - will be automatically updated.

For newly added methods, I don't know of a way to have the test class automatically updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文