unit_tests
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
unit_tests [2016/09/08 17:41] – mhatz | unit_tests [2023/04/25 16:52] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 5: | Line 5: | ||
For Mail2Voice Next, we introduced unit tests. Units tests are useful to ensure that every classes/ | For Mail2Voice Next, we introduced unit tests. Units tests are useful to ensure that every classes/ | ||
- | The unit tests written for Mail2Voice are base on QTest suite. | + | The unit tests written for Mail2Voice are based on QTest suite. |
===== HOWTO writing a unit test for a class ===== | ===== HOWTO writing a unit test for a class ===== | ||
- | Each class of Mail2Voice must have its dedicated unit tests to check every method | + | Each class of Mail2Voice must have its dedicated unit tests to check every methods |
==== Example ==== | ==== Example ==== | ||
Line 34: | Line 34: | ||
</ | </ | ||
- | MyClass is a very simple class that does two things : incrementing an internal variable and setting an internal text. Nothing special | + | MyClass is a very simple class that does two things : incrementing an internal variable and setting an internal text. Nothing special |
- | 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 | + | * For our example, we will pretend that m_count must be initialized |
- | * increment(): | + | * increment(): |
* 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& | + | * setText(const QString& |
* count() and text() methods must always return the current value. | * count() and text() methods must always return the current value. | ||
Line 88: | Line 88: | ||
</ | </ | ||
- | The PropsTester class is a facility | + | The PropsTester class is a convenience |
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.1473356461.txt.gz · Last modified: 2023/04/25 16:52 (external edit)