我如何改善我的junit测试 [英] How can I improve my junit tests

查看:70
本文介绍了我如何改善我的junit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对,我的junit测试看起来很长话:

Right my junit tests look like a long story:

  • 我创建了4个用户
  • 我删除了1个用户
  • 我尝试使用已删除的用户登录,并确保失败
  • 我使用剩余的3个用户之一登录并确认我可以登录
  • 我从一个用户向另一个用户发送一条消息,并验证该消息是否出现在发件人的发件箱和收件人的收件箱中.
  • 我删除了邮件
  • ...
  • ...

优势: 测试非常有效(非常善于检测错误)并且非常稳定,因为它们仅使用API​​,如果我重构代码,那么测试也将被重构.由于我不使用肮脏的把戏",例如以给定状态保存和重新加载数据库,因此我的测试忽略了架构更改和实现更改.

Advantages: The tests are quite effective (are very good at detecting bugs) and are very stable, becuase they only use the API, if I refactor the code then the tests are refactored too. As I don't use "dirty tricks" such as saving and reloading the db in a given state, my tests are oblivious to schema changes and implementation changes.

缺点: 测试变得难以维护,测试中的任何更改都会影响其他测试.测试运行8-9分钟,这对于持续集成非常有用,但对开发人员来说却有些沮丧.测试不能孤立运行,最好的办法是在您感兴趣的测试运行后停止-但您绝对必须运行之前进行的所有测试.

Disadvantages: The tests are getting difficult to maintain, any change in a test affects other tests. The tests run 8-9 min which is great for continuous integration but is a bit frustrating for developers. Tests cannot be run isolated, the best you can do is to stop after the test you are interested in has run - but you absolutely must run all the tests that come before.

您将如何改进我的考试?

How would you go about improving my tests?

推荐答案

单元测试-理想情况下-应该是独立的,并且能够以任何顺序运行.所以,我建议你:

unit tests should - ideally - be independent, and able to run in any order. So, I would suggest that you:

  • 分解测试以保持独立性
  • 考虑使用内存数据库作为测试的后端
  • 考虑将每个测试或套件包装在最终回滚的事务中
  • 分析单元测试以查看时间,然后集中精力

如果创建一些用户并发送一些消息需要8分钟,则性能问题可能不在测试中,而是可能是系统本身存在性能问题的征兆-只有您的探查器才能确定!

if it takes 8 minutes to create a few users and send a few messages, the performance problem may not be in the tests, rather this may be a symptom of performance problems with the system itself - only your profiler knows for sure!

[caveat:尽管我可能只是少数,但我不认为这些测试是集成测试";我认为这类测试是功能的单元测试,大约是TDD]

[caveat: i do NOT consider these kinds of tests to be 'integration tests', though i may be in the minority; i consider these kinds of tests to be unit tests of features, a la TDD]

这篇关于我如何改善我的junit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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