Summary

This presentation covers various aspects of software testing, specifically focusing on unit testing. It details the definition, steps, and frameworks associated with unit testing, along with important topics like test coverage and the use of assertions in C++. The presentation also briefly touches upon setting up test projects, running tests within Visual Studio, and running them from a command line.

Full Transcript

Software Testing Unit Testing Instructor: Kaveh Eshraghian Unit Testing Definition:  Unit testing is the process of testing the code in small units. What to Test:  Usually, these units correspond to functions Steps to Test: 1. Feeding the function under tes...

Software Testing Unit Testing Instructor: Kaveh Eshraghian Unit Testing Definition:  Unit testing is the process of testing the code in small units. What to Test:  Usually, these units correspond to functions Steps to Test: 1. Feeding the function under test specific values. 2. Gathers a return value and compares it to expected output. 3. If values produced by the function under test is identical to that that is expected, test has passed Test Frameworks Unit test framework advantages:  Allows us to set up the test,  Call the function being tested and store the result,  Compare the result to the expected result  Use an assertion to throw an exception if the test fails  Tear down the test Test Check Test Execute Setup result Teardown Test Coverage Definition:  The extent to which the source code of a software application is tested by a set of test cases. It is a metric that helps evaluate the effectiveness and thoroughness of testing efforts. What to Test:  Measures which line of code were tested.  The number of times each line was executed. Steps to Test: 1. Compile code with counting flags (gcc -Wall -fprofile-arcs -ftest-coverage -o insert insert.c) 2. Execute program. 3. Prepare report (gcov insert.c) 4. Open file insert.c.gcov Test Math Functions mathfuncs.h mathfuncs.c Create a Test Project Write Test Code #include "pch.h" #include "CppUnitTest.h" TEST_CLASS(MathIntegrationTest) #include "mathfuncs_r.h" { using namespace public: Microsoft::VisualStudio::CppUnitTestFramework; namespace MathTestSuite { TEST_METHOD(AdditionTest) TEST_CLASS(MathTest) { { public: double d = square(8.0); TEST_METHOD(SquareTest) double d1 = cube(3.0); { double d = square(8.0); Assert::AreEqual(91.0, d + d1); } Assert::AreEqual(64.0, d); } TEST_METHOD(CubeTest) }; { double d = cube(3.0); } Assert::AreEqual(27.0, d); } }; Access C Include File Assertions Method Description Compares v1 to v2 and throws an exception if they are not equal. If they AreEqual(v1, v2 [, "error message"] ) are not equal, the optional error string will be displayed. Compares v1 to v2 and throws an AreNotEqual(v1, v2 [, "error exception if they are equal. If they are message"] ) equal, the optional error string will be displayed. If the Boolean b1 is not true it throws IsTrue(b1 [, "error message"] ) an exception. If not true, the optional error string will be displayed. If the Boolean b1 is not false it throws IsFalse(b1 [, "error message"] ) an exception. If not false, the optional error string will be displayed. This causes the test to fail and throws Fail([ "error message"] ) an exception. If called, the optional error string will be displayed. Setup Library Path Setup Include Path Running Tests in Visual Studio Go to the Test menu at the top and select Test Explorer. This will display a new window to control the tests, as shown below. Test Results Setup and Teardown Before/After Tests Setup and Teaddown for Each Test Test from Command Line vstest.console.exe MathProj-UnitTest.dll

Use Quizgecko on...
Browser
Browser