开发者

Symfony unit test best practice

开发者 https://www.devze.com 2023-02-13 06:15 出处:网络
I would like to know what are the best practices to manage fixtures of unit tests. I have the feeling that I should write one fixture foreach unit test.

I would like to know what are the best practices to manage fixtures of unit tests.

I have the feeling that I should write one fixture foreach unit test. I think that if I write too much unit test the fixture will be too long. Then if I want to modify it I will break some fixtures.

Now I search a way to write one fixture per unit test.

In theory there is no problem I think. Tell me if I'm wrong.

My problem is how can I ensure that fixtures will be updated. I don't want to change all fixtur开发者_JAVA百科es if I change the database schema. It should be possible if any changes of the schema are making with Migration.

Is there any tools for that ?


I found it better to use separate fixture file for each test file. This approach has several advantages:

  • Fixtures are independent and changing them doesn't influence all the tests but just one.
  • Fixtures are smaller as they don't need to cover every test case. This makes them easier to read.
  • It's easier to see what's the subject of a test case as each fixture file covers just enough data.

The drawback is that when you change the database often you need to update your fixtures as well. In many cases it is not needed as new field often is used with new tests only. Otherwise you can always automate the process (I do it with vim macros for example).

Adding new field shouldn't break your existing tests IMHO. You should make that all the tests still pass. If you need to change the behavior you should update your tests first, make them fail and than make them pass.

0

精彩评论

暂无评论...
验证码 换一张
取 消