C# example
Step #1 - Write the code for all possible invalid condition.
//should not allow empty string or null string public bool SetName(string strName) { bool blnStatus = false; if (strName != string.Empty && strName != null) { } return blnStatus; }
Step #2 - Perform a quick unit test.
Example unit test cases.
- Test the code for empty string and expect the function to return false
- Test the code for null string and expect the function to return false
- If any of the above case fails fix the code without writing the code for invalid case
- When all the above cases succeed write the code for valid case
Step #3 - Fix the defects and verify the fixes if any.
C# example
Step #4 - Write the code for all valid conditions.
//should not allow empty string or null string public bool SetName(string strName) { bool blnStatus = false; if (strName != string.Empty && strName != null) { m_strName = strName; blnStatus = true; } return blnStatus; }
Step #5 - Perform the unit test for the entire functionality.
Example unit test cases.
- Test the code for empty string and expect the function to return false
- Test the code for null string and expect the function to return false
- Next add the test case for valid string value and expect the function to return true
- If any of the above case fails fix the code and verify the same
Step #6 - Fix the defects and verify the fixes if any.
Write invalid condition code → Unit test → Fix & Verify → Write valid condition code → Unit test → Fix & Verify
Note: Unit testing automation strategy has to planned well before starting the implementation. There are unit testing automation frameworks available in the open source community as well as COTS.
Rule: Unit test the function.
No comments:
Post a Comment