User Tools

Site Tools


unit_tests

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
unit_tests [2016/09/08 17:43] – [HOWTO writing a unit test for a class] mhatzunit_tests [2016/09/09 07:38] mhatz
Line 34: Line 34:
 </code> </code>
  
-MyClass is a very simple class that does two things : incrementing an internal variable and setting an internal text. Nothing special there, isn't it? Why should we care about testing such a trivial code?+MyClass is a very simple class that does two things : incrementing an internal variable and setting an internal text. Nothing special here, isn't it? Why should we care about testing such a trivial code?
  
-Wait, mistakes are common and even if you are sure of your code, are you sure that no one else won't introduce errors in your code? That, is where unit tests are useful : ensuring your code is correct and will stay correct over time. Saving you a lot of time of debugging in the future.+Wait, mistakes are common and even if you are sure of your code, are you sure that no one else won't introduce errors in your code? That, is where unit tests are useful: ensuring your code is correct and will stay correct over time. Saving you a lot of time of debugging in the future.
  
 ==== What to test? ==== ==== What to test? ====
Line 50: Line 50:
  
   * MyClass(): the constructor must initialize the object, so it must initialize the members m_count and m_text. But, to which values? This have to be specified somewhere otherwise the objects could be inconsistent.   * MyClass(): the constructor must initialize the object, so it must initialize the members m_count and m_text. But, to which values? This have to be specified somewhere otherwise the objects could be inconsistent.
-    * For our example, we will pretend that m_count must be initialize to 0 and m_text to "default text";+    * For our example, we will pretend that m_count must be initialized to 0 and m_text to "Default text";
  
-  * increment(): this must add just 1 to m_count. But m_count is an int, meaning it can be negative. What if we increment m_count until 2147483647 (assuming int is on 32 bits) and the increment again? Does the increment method will just put m_count to the minimum negative value? Or will it refuse to increment again, leaving m_count to 2147483647?+  * increment(): this must add just 1 to m_count. But m_count is an int, meaning it can be negative. What if we increment m_count until 2147483647 (assuming int is on 32 bits) and then increment again? Does the increment method will just put m_count to the minimum negative value (-2147483648)? Or will it refuse to increment again, leaving m_count to 2147483647?
     * For our example, we choose the second option: never go to negative value.     * For our example, we choose the second option: never go to negative value.
  
-  * setText(const QString& text): now you get it, we will assume this method accept any strings except empty ones.+  * setText(const QString& text): now you get it, we will assume this method accept any strings except empty ones (it will do nothing in this case).
  
   * count() and text() methods must always return the current value.   * count() and text() methods must always return the current value.
Line 88: Line 88:
 </code> </code>
  
-The PropsTester class is a facility to check properties against their default values. While it is not mandatory, it is highly recommended to ease the test of class members integrity.+The PropsTester class is a convenience to check properties against their default values. While it is not mandatory, it is highly recommended to ease the test of class members integrity.
  
 The implementation of TestMyClass will look like this: The implementation of TestMyClass will look like this:
Line 129: Line 129:
     MyClass myClass;     MyClass myClass;
          
-    for(int i = 1; i <2147483647; ++i)+    for(int i = 0; i < 2147483647; ++i)
     {     {
         myClass.increment();         myClass.increment();
unit_tests.txt · Last modified: 2023/04/25 16:52 by 127.0.0.1