How can I improve my junit tests

后端 未结 9 836
南笙
南笙 2020-12-05 08:59

Right my junit tests look like a long story:

  • I create 4 users
  • I delete 1 user
  • I try to login with the deleted user and make sure it fails
9条回答
  •  半阙折子戏
    2020-12-05 09:25

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

    • break up your tests to be independent
    • consider using an in-memory database as the backend for your tests
    • consider wrapping each test or suite in a transaction that is rolled back at the end
    • profile the unit tests to see where the time is going, and concentrate on that

    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: 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]

提交回复
热议问题