Search code examples
unit-testing

Is it useless to do unit tests after writing code?


I finished an app and after that I'm trying to write unit test to cover all methods.

The thing is that I'm seeing that I'm testing my code with the knowledge of how it works.

For me it is a bit stupid because I know how the code works and I'm testing what my code is actually doing.

Is this useless? I'm testing what it does, not what it's supposed to do. My code works but I can improve it. Then:

Should I complete all my tests and then try to refactor my code changing my test to "How the code should work" and making the changes to the app to pass the test?


Solution

  • You need to test "How the code should work". That is, you have a clear idea of what a particular method should behave like, then create the set of tests that covers that behaviour. If your code fails the test then you can fix it.

    Even though your code is working now you still need the tests for regression testing. Later when you modify your code or add new extensions to it you need to ensure that you did not break the existing functionality. The set of tests that you derive today will be able to tell you how well you did.