Unit Testing: Beginner Questions

后端 未结 4 750
陌清茗
陌清茗 2021-01-31 12:14

I\'m finally starting out with unit testing, having known that I should be doing it for a while, but I have a few questions:

  • Should or shouldn\'t I retest parent c
4条回答
  •  情歌与酒
    2021-01-31 12:55

    I'll answer the ones that I can.

    Should or shouldn't I retest parent classes when testing the children if no methods have been overwritten?

    You should fully test the parent, then test only what changes in the child.

    If you have optional parameter in a method, should you write a test for both when they are present and when they are not?

    Yes, test anything that causes a change in behavior.

    Should unit testing in any way be combined with testing code execution time or should they remain completely separate?

    They should remain separate. Unit testing is to test that a method does what it's supposed to. You should test code execution time at a system level, then break it down to find bottlenecks. Testing the performance of each individual unit will only lead to premature optimization.

    Is there any valid reason not to run your full test suite every time?

    If your test suite is huge and takes a long time, you might want to only run a subset of it while you're still developing. You should run the entire suite when you (think you) are done to make sure you didn't break anything else.

    Just so I'm getting my terminology right, to what does the unit in unit testing refer? The class being tested? The method? The parameter? Something else?

    "Unit" refers to the method being tested. This is the smallest unit that it makes sense to break software down to. A method for class A might use class B, but any test you write for that method shouldn't care. Just test that method.

提交回复
热议问题