Why are functional tests not enough? What do unit tests offer?

前端 未结 8 1126
庸人自扰
庸人自扰 2020-12-25 09:02

I just had a conversation with my lead developer who disagreed that unit tests are all that necessary or important. In his view, functional tests with a high enough code cov

相关标签:
8条回答
  • 2020-12-25 09:31

    unit tests are for devs to see where the code failed

    functional tests are for the business to see if the code does what they asked for

    unit tests are checking that you've manufactured your bricks correctly

    functional tests are checking that the house meets the customer's needs.

    They're different things, but the latter will be much easier, if the former has been carried out.

    0 讨论(0)
  • 2020-12-25 09:34

    Off the top of my head

    • Unit tests are repeatable without effort. Write once, run thousands of times, no human effort required, and much faster feedback than you get from a functional test
    • Unit tests test small units, so immediately point to the correct "sector" in which the error occurs. Functional tests point out errors, but they can be caused by plenty of modules, even in co-operation.
    • I'd hardly call an interface change "an inner refactoring". Interface changes tend to break a lot of code, and (in my opinion) force a new test loop rather than none.
    0 讨论(0)
  • 2020-12-25 09:44

    If you use a pure Extreme Programing / Agile Development methodology the Unit tests are always required as they are the requirements for development.

    In pure XP/Agile one makes all requirements based on the tests which are going to be performed to the application

    • Functional tests - Generate functional requirements.
    • Unit tests - Generate functions or object requirements.

    Other than that Unit testing can be used to keep a persistent track of function requirements.

    i.e. If you need to change the working way of a function but the input fields and output keep untouched. Then unit testing is the best way to keep tracking of possible problems as you only need to run the tests.

    0 讨论(0)
  • 2020-12-25 09:46

    Assume for a second that you already have a thorough set of functional tests that check every possible use case available and you are considering adding unit tests. Since the functional tests will catch all possible bugs, the unit tests will not help catch bugs. There are however, some tradeoffs to using functional tests exclusively compared to a combination of unit tests, integration tests, and functional tests.

    • Unit tests run faster. If you've ever worked on a big project where the test suite takes hours to run, you can understand why fast tests are important.
    • In my experience, practically speaking, functional tests are more likely to be flaky. For example, sometimes the headless capybara-webkit browser just can't reach your test server for some reason, but you re-run it and it works fine.
    • Unit tests are easier to debug. Assuming that the unit test has caught a bug, it's easier and faster to pinpoint exactly where the problem is.

    On the other hand, assuming you decide to just keep your functional tests and not add any unit tests

    • If you ever need to re-architect the entire system, you may not have to rewrite any tests. If you had unit tests, a lot of them will probably be deleted or rewritten.
    • If you ever need to re-architect the entire system, you won't have to worry about regressions. If you had relied on unit tests to cover corner cases, but you were forced to delete or rewrite those unit tests, your new unit tests are more likely to have mistakes in them than the old unit tests.
    • Once you already have the functional test environment set up and you have gotten over the learning curve, writing additional functional tests is often easier to write and often easier to write correctly than a combination of unit tests, integration tests, and functional tests.
    0 讨论(0)
  • 2020-12-25 09:48

    unit tests are for devs to see where the code failed

    functional tests are for the business to see if the code does what they asked for

    0 讨论(0)
  • 2020-12-25 09:49

    Bugs should be caught as soon as possible in the development cycle - having bugs move from design to code, or code to test, or (hopefully not) test to production increases the cost and time required to fix it.

    Our shop enforces unit testing for that reason alone (I'm sure there are other reasons but that's enough for us).

    0 讨论(0)
提交回复
热议问题