Flex best practices – Part 2: Development practices | Adobe Developer ConnectionUnit testing can ensure the quality of your codebase. Writing test cases can lead to more reliable and better code. Testing your code will help find errors and save debugging time. Here are a few best practices that can be applied when implementing unit testing.
Test behavior instead of testing methods
Writing code to test private methods is not always needed. This should be evaluated on a case by case basis. Write code to test against an object's public API. Typically private methods are used internally by a class to support its public API.
Apply the "too simple to break" rule
Don't over test the codebase. If the method is a very simple one line method, like a getter method, don't worry about writing a test case for it.
Use standard OOP best practices in test cases
Follow the same OO methodology you use to write the rest of your application. Refactor test cases to move common functionality into base classes as appropriate.
Use clear and concise test method names
Follow the same method naming conventions used elsewhere in the application's codebase. Avoid generic names for test case methods. This helps other developers easily understand the relationship, responsibility, and purpose of the method.
Write simple test case methods
Keep test cases simple and limit the decisions that are made within them.
Use static values in the assertion method calls when possible
This reduces the chances for error in your test cases and improves the maintainability and understandability of the test code.
Document the test code
Add comments that explain what is being tested and why. Also, outline any special circumstances regarding the test.
Create independent unit tests
Each unit test should execute a single behavior for each method.
Limit assertions to one per test case
Multiple assertions in a single test case can lead to performance issues. In addition, if one assertion fails the other assertions will not be executed until the first assertion passes.