![Page 2: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/2.jpg)
Recall the rules1. Understand the requirements 2. Make it fail 3. Simplify the test case 4. Read the right error message 5. Check the plug 6. Separate fact from fiction 7. Divide and conquer 8. Match the tool to the bug 9. One change at a time 10. Keep an audit trail 11. Get a fresh view 12. If you didn’t fix it, it ain’t fixed 13. Cover your bug fix with a regression test
![Page 3: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/3.jpg)
Recall the rules1. Understand the requirements 2. Make it fail 3. Simplify the test case 4. Read the right error message 5. Check the plug 6. Separate fact from fiction 7. Divide and conquer 8. Match the tool to the bug 9. One change at a time 10. Keep an audit trail 11. Get a fresh view 12. If you didn’t fix it, it ain’t fixed 13. Cover your bug fix with a regression test
![Page 4: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/4.jpg)
Recall the rules1. Understand the requirements 2. Make it fail 3. Simplify the test case 4. Read the right error message 5. Check the plug 6. Separate fact from fiction 7. Divide and conquer 8. Match the tool to the bug 9. One change at a time 10. Keep an audit trail 11. Get a fresh view 12. If you didn’t fix it, it ain’t fixed 13. Cover your bug fix with a regression test
![Page 5: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/5.jpg)
Unit testing frameworks
uniform way of expressing tests
manage tests through suites
automate testing process
![Page 6: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/6.jpg)
Remember JUnit?
![Page 7: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/7.jpg)
We'll use CUnit
http://cunit.sourceforge.net
http://cunit.sourceforge.net/doc
![Page 8: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/8.jpg)
http://cunit.sourceforge.net/doc/introduction.html
![Page 9: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/9.jpg)
http://cunit.sourceforge.net/doc/introduction.html
![Page 10: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/10.jpg)
a test
a void -> void method
test methods must be part of a test suite, which must in turn be registered with registry, before it will be run
![Page 11: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/11.jpg)
Assertions (the most common ones)
CU_ASSERT_TRUE(x) CU_ASSERT_EQUAL(x,y) CU_ASSERT_PTR_EQUAL(x,y) CU_ASSERT_PTR_NULL(x,y) CU_ASSERT_STRING_EQUAL(x,y) CU_ASSERT_DOUBLE_EQUAL(x,y,𝜀) CU_PASS(message)
CU_ASSERT_FALSE(x) CU_ASSERT_NOT_EQUAL(x,y) CU_ASSERT_PTR_NOT_EQUAL(x,y) CU_ASSERT_PTR_NOT_NULL(x,y) CU_ASSERT_STRING_NOT_EQUAL(x,y) CU_ASSERT_DOUBLE_NOT_EQUAL(x,y,𝜀) CU_FAIL(message)
http://cunit.sourceforge.net/doc/headers/CUnit.h
![Page 12: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/12.jpg)
LIVE EXAMPLE
![Page 13: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/13.jpg)
Live coding reconstruction
Production code
Test code
Test code is separate from production code, but calls production code to verify its functionality.
![Page 14: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/14.jpg)
Live coding reconstruction
void test00(void) { int x = 5; int y = -3; int expected = 2; int actual = eval(x, y); CU_ASSERT_EQUAL( expected , actual ); }
A test is a void to void function. Set up inputs and an expected (correct) answer. Compute an actual answer using production code. Compare actual and expected values.
![Page 15: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/15.jpg)
Live coding reconstruction
void test01(void) { runTest(1,1,2); } void test02(void) { runTest(2,1,3); } void test03(void) { runTest(0,2,2); } void test04(void) { runTest(2,0,2); }
/* Runs a test with inputs that *should* pass * and produce an expected result. */ void runTest(int x, int y, int expected) { int actual = eval(x, y); CU_ASSERT_EQUAL( expected , actual ); }
Can simplify writing many tests by using a helper function, runTest.
![Page 16: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/16.jpg)
Live coding reconstruction
Needed #includes:
#include "CUnit.h" #include "Basic.h"
![Page 17: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/17.jpg)
Live coding reconstruction
/* The main() function for setting up and running the tests. */ int main() { CU_pSuite SuiteValid = NULL; /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) { return CU_get_error(); } /* add a suite to the registry */ SuiteValid = CU_add_suite("Suite_of_tests_with_valid_inputs", NULL, NULL); if (NULL == SuiteValid) { CU_cleanup_registry(); return CU_get_error(); } /* add the tests to SuiteValid */ if ( (NULL == CU_add_test(SuiteValid, "5+(-3)=2", test00)) || (NULL == CU_add_test(SuiteValid, "1+1=2", test01)) ) { CU_cleanup_registry(); return CU_get_error(); }
/* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests();
CU_cleanup_registry(); return CU_get_error(); }
![Page 18: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/18.jpg)
Live coding reconstruction
/* The main() function for setting up and running the tests. */ int main() { CU_pSuite SuiteValid = NULL; /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) { return CU_get_error(); } /* add a suite to the registry */ SuiteValid = CU_add_suite("Suite_of_tests_with_valid_inputs", NULL, NULL); if (NULL == SuiteValid) { CU_cleanup_registry(); return CU_get_error(); } /* add the tests to SuiteValid */ if ( (NULL == CU_add_test(SuiteValid, "5+(-3)=2", test00)) || (NULL == CU_add_test(SuiteValid, "1+1=2", test01)) ) { CU_cleanup_registry(); return CU_get_error(); }
/* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests();
CU_cleanup_registry(); return CU_get_error(); }
Create a test suite. In this case the suite is named SuiteValid.
![Page 19: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/19.jpg)
Live coding reconstruction
/* The main() function for setting up and running the tests. */ int main() { CU_pSuite SuiteValid = NULL; /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) { return CU_get_error(); } /* add a suite to the registry */ SuiteValid = CU_add_suite("Suite_of_tests_with_valid_inputs", NULL, NULL); if (NULL == SuiteValid) { CU_cleanup_registry(); return CU_get_error(); } /* add the tests to SuiteValid */ if ( (NULL == CU_add_test(SuiteValid, "5+(-3)=2", test00)) || (NULL == CU_add_test(SuiteValid, "1+1=2", test01)) ) { CU_cleanup_registry(); return CU_get_error(); }
/* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests();
CU_cleanup_registry(); return CU_get_error(); }
Register each test function with the SuiteValid test suite here.
If you add more test functions for this suite, add code here to register each test function.
![Page 20: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/20.jpg)
Live coding reconstruction
/* The main() function for setting up and running the tests. */ int main() { CU_pSuite SuiteValid = NULL; /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) { return CU_get_error(); } /* add a suite to the registry */ SuiteValid = CU_add_suite("Suite_of_tests_with_valid_inputs", NULL, NULL); if (NULL == SuiteValid) { CU_cleanup_registry(); return CU_get_error(); } /* add the tests to SuiteValid */ if ( (NULL == CU_add_test(SuiteValid, "5+(-3)=2", test00)) || (NULL == CU_add_test(SuiteValid, "1+1=2", test01)) || (NULL == CU_add_test(SuiteValid, "1+2=3", test02)) || (NULL == CU_add_test(SuiteValid, "0+2=2", test03)) || (NULL == CU_add_test(SuiteValid, "2+0=2", test04)) ) { CU_cleanup_registry(); return CU_get_error(); }
/* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); }
This is what it looks like with more test functions registered.
![Page 21: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/21.jpg)
Branching and merging in git
A sequence of commits all in the master branch.
commit 1
commit 2
commit 3 master
HEAD
![Page 22: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/22.jpg)
Branching and merging in git
Create a new branch: git branch bugfix
commit 1
commit 2
commit 3 master
HEAD
bugfix
![Page 23: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/23.jpg)
Branching and merging in git
Move HEAD to new branch git checkout bugfix
commit 1
commit 2
commit 3 master
HEAD
bugfix
![Page 24: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/24.jpg)
Branching and merging in git
Make a commit on bugfix branch
commit 1
commit 2
commit 3 master
HEAD
bugfixcommit
4
![Page 25: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/25.jpg)
Branching and merging in git
Make another commit on bugfix
branch
commit 1
commit 2
commit 3 master
HEAD
bugfixcommit
4
commit 5
![Page 26: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/26.jpg)
Branching and merging in git
Check out master again
commit 1
commit 2
commit 3 master
HEAD
bugfixcommit
4
commit 5
![Page 27: CSE306 Software Quality in Practice - University at Buffalo · CSE306 Software Quality in Practice Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall](https://reader036.vdocument.in/reader036/viewer/2022081401/5f07cce47e708231d41ecf78/html5/thumbnails/27.jpg)
Branching and merging in git
Merge bugfix into master. Since no other commits were made to master in between this
merge is straightforward.
commit 1
commit 2
commit 3 master
HEAD
bugfix
commit 4
commit 5