Test Class Sample: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
 
(65 intermediate revisions by 8 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==


The following content relates to the sample files and workspaces installed in ''%STRIDE_DIR%\Samples\TestUnits\TestClass''.  This sample consists of a [http://en.wikipedia.org/wiki/Microsoft_Visual_Studio Visual Studio] workspace for building an [[Windows_Off-Target_Apps| off-target simulator]], sample [[Test Units|test class]] source code, and a STRIDE workspace for doing more advanced test class execution.
These examples cover the use of [[Scl test class|Test Classes]] to create logical groupings of test cases.  For unit testing, one test class is typically created for each class under test, although more complicated scenarios often justify other arrangements. Test Classes require a c++ compiler.


==Getting Started==
''NOTE:'' each of the example test classes is grouped in namespace corresponding to its category (e.g. ''Basic'' or ''Runtime Services'').  This is something shown for organizational purposes only -- it is '''not''' a general requirement that test classes be placed into namespaces. 


To begin, open the Visual Studio Solution file in the sample directory.  This solution (and corresponding project) were created for Visual Studio 2005.  If you have a later version of Visual Studio installed, you should be able to open this solution and it will be automatically upgraded if necessary.  If you do not currently have any version of Visual Studio, we recommend that you install the current free (as in beer) version of [http://en.wikipedia.org/wiki/Visual_Studio_Express Visual Studio Express].
==Tests Description==


Once you have successfully opened the solution, rebuild it.  The build process has custom STRIDE build rules integrated and will produce a STRIDE database, intercept module source files, and an off-target simulator application that incorporates the test class source.
===Basic===


Once the build is complete, perform the following steps to run the test classes in the workspace:
These examples cover the simplest, easiest way to code a STRIDE test class. These examples use simple [http://en.wikipedia.org/wiki/Plain_Old_Data_Structures POD] return types to indicate status.


# launch the off-target simulator, TestClass.exe.  This will run in a standard console window.
====Basic::Simple====
# open a command prompt window and change to this sample's directory.
# at the command prompt, run the command <tt>TestUnitRun.pl -v</tt>.  This will execute all of the test units in the workspace and open a browser to display the results.
# quit the TestClass.exe application by typing 'q' in its console window.


==Sample Test Classes==
This example demonstrates passing and failing tests using the four primary integer types (int, bool, short, and char) that infer status from.


Now that you have built the off-target simulator and run the test classes it contains, you can take time to peruse the test class source and the corresponding results that each produces.  This section provides a brief description for each.
====Basic::Fixtures====


===Basic Examples===
This example demonstrates how to use [[Test_Unit_Pragmas#Fixturing_Pragmas|setup and teardown]] fixtures.  The setup and teardown methods are called immediately before and after the execution of each test method, respectively.


====01_01_Basic_Simple====
====Basic::Exceptions====


====01_02_Basic_Fixtures====
This example demonstrates exceptions thrown from a test method are caught by the intercept module and noted in the results.  Any exception that is caught by the harness is assumed to indicate failure. If you want to write tests for expected exceptions, consider using our [[Test Macros#Exception_Macros|exception macros]]


====01_03_Basic_Exceptions====
====Basic::Parameterized====


====01_04_Basic_Constructors====
This example demonstrates how to pass arguments to the constructor of your test unit. This is something that is useful when you want to run the same test scenario with different sets of input data, for instance, as described by [http://xunitpatterns.com/Parameterized%20Test.html this pattern].


===Runtime Services Examples===
===Runtime Services===


====02_01_RuntimeServices_Simple====
These examples cover basic usage of the [[Runtime_Test_Services#C_Test_Functions|Runtime Test Services API]] (as declared in srtest.h).


====02_02_RuntimeServices_Dynamic====
====RuntimeServices::Simple====


====02_03_RuntimeServices_Override====
This example demonstrates how to use [[Runtime_Test_Services#srTestCaseSetStatus|srTestCaseSetStatus]] to set status and [[Runtime_Test_Services#srTestCaseAddAnnotation|srTestCaseAddAnnotation]] to add a comment.


====02_04_RuntimeServices_VarComment====
====RuntimeServices::Dynamic====


===srTest Examples===
This example demonstrates how to use [[Runtime_Test_Services#srTestSuiteAddCase|srTestSuiteAddCase]], [[Runtime_Test_Services#srTestSuiteAddAnnotation|srTestSuiteAddAnnotation]], and [[Runtime_Test_Services#srTestAnnotationAddComment|srTestAnnotationAddComment]] for dynamic case, and annotation creation in the context of a single test method.


====03_01_srTest_Simple====
====RuntimeServices::Override====


====03_02_srTest_Dynamic====
This example demonstrates how to use [[Runtime_Test_Services#srTestCaseSetStatus|srTestCaseSetStatus]] to override the status that would otherwise be inferred from the return value.


==Test Class Execution==
====RuntimeServices::VarComment====


===Command Line Execution===
This example demonstrates the use of [http://en.wikipedia.org/wiki/Printf printf] style format strings with [[Runtime_Test_Services#srTestCaseAddAnnotation|srTestCaseAddAnnotation]].


===Workspace-Based Execution===
===srTest===
 
These examples show to how to use the [[Runtime_Test_Services#C.2B.2B_Test_Classes|stride::srTest]] base class for your test classes.  When you publicly inherit from srTest, you get access to default testCase and testSuite members and their associated methods.
 
====srTest::Simple====
 
This example demonstrates the use of [[Runtime_Test_Services#SetStatus|testCase.setStatus]] to set the status for test cases.
 
====srTest::Dynamic====
 
This example demonstrates how to use [[Runtime_Test_Services#method_AddCase|AddCase]], [[Runtime_Test_Services#method_AddAnnotation_2|testCase.AddAnnotation]], and [[Runtime_Test_Services#method_AddComment|testAnnotation.AddComment]] for dynamic test case, and annotation creation within the context of one test method.
 
== Run Tests ==
 
Now launch the test app (if you have not already) and execute the runner  with the following commands:
 
''Test Class tests'':
<pre>
stride --device="TCP:localhost:8000" --database="../out/TestApp.sidb" --run="s2_testclass::Basic::Exceptions; s2_testclass::Basic::Fixtures;  s2_testclass::Basic::Parameterized; s2_testclass::Basic::Simple" --output=TestClass.xml
</pre>
 
Note the command will produce distinct result files for the run (per the ''--output'' command above). Please use the result file to peruse the results by opening each in your browser.
 
== Observations ==
This sample shows the techniques available for packaging and writing test units using classes. If you have a C++ capable compiler, we recommend that you use test classes to package your unit tests, even if your APIs under test are C only. Review the source code in the directory and follow the sample description.
 
The following are some test observations:
* all of these example classes have been put into one or more namespaces. This is just for organization purposes, mainly to avoid name collisions when built along with lots of other test classes. Your test classes are '''not''' required to be in namespaces, but it can be helpful in avoiding collisions as the number of tests in your system grows.
* we've documented our test classes and methods using [https://en.wikipedia.org/wiki/Doxygen doxygen] style comments. This documentation is automatically extracted by our tools and added to the results report - more information about this feature is [[Test_Documentation_in_C/C%2B%2B|here]]
* you can optionally write test classes that inherit from a base class that we've defined ([[Runtime_Test_Services#class_srTest|stride::srTest]]). We recommend you start by writing your classes this way so that your classes will inherit some methods and members that make some custom reporting tasks simpler.
* exceptions are generally handled by the STRIDE unit test harness, but can be disabled if your compiler does not support them (see ''s2_testclass_basic_exceptions_tests.h/cpp'').
* parameterized tests are supported by test classes as well. In these tests, simple constructor arguments can be passed during execution and are available at runtime to the test unit. The STRIDE infrastructure handles the passing of the arguments to the device and the construction of the test class with these arguments. Parameterization of test classes can be a powerful way to expand your test coverage with data driven test scenarios (varying the input to a single test class).




[[Category: Samples]]
[[Category: Samples]]

Latest revision as of 19:00, 28 December 2018

Introduction

These examples cover the use of Test Classes to create logical groupings of test cases. For unit testing, one test class is typically created for each class under test, although more complicated scenarios often justify other arrangements. Test Classes require a c++ compiler.

NOTE: each of the example test classes is grouped in namespace corresponding to its category (e.g. Basic or Runtime Services). This is something shown for organizational purposes only -- it is not a general requirement that test classes be placed into namespaces.

Tests Description

Basic

These examples cover the simplest, easiest way to code a STRIDE test class. These examples use simple POD return types to indicate status.

Basic::Simple

This example demonstrates passing and failing tests using the four primary integer types (int, bool, short, and char) that infer status from.

Basic::Fixtures

This example demonstrates how to use setup and teardown fixtures. The setup and teardown methods are called immediately before and after the execution of each test method, respectively.

Basic::Exceptions

This example demonstrates exceptions thrown from a test method are caught by the intercept module and noted in the results. Any exception that is caught by the harness is assumed to indicate failure. If you want to write tests for expected exceptions, consider using our exception macros

Basic::Parameterized

This example demonstrates how to pass arguments to the constructor of your test unit. This is something that is useful when you want to run the same test scenario with different sets of input data, for instance, as described by this pattern.

Runtime Services

These examples cover basic usage of the Runtime Test Services API (as declared in srtest.h).

RuntimeServices::Simple

This example demonstrates how to use srTestCaseSetStatus to set status and srTestCaseAddAnnotation to add a comment.

RuntimeServices::Dynamic

This example demonstrates how to use srTestSuiteAddCase, srTestSuiteAddAnnotation, and srTestAnnotationAddComment for dynamic case, and annotation creation in the context of a single test method.

RuntimeServices::Override

This example demonstrates how to use srTestCaseSetStatus to override the status that would otherwise be inferred from the return value.

RuntimeServices::VarComment

This example demonstrates the use of printf style format strings with srTestCaseAddAnnotation.

srTest

These examples show to how to use the stride::srTest base class for your test classes. When you publicly inherit from srTest, you get access to default testCase and testSuite members and their associated methods.

srTest::Simple

This example demonstrates the use of testCase.setStatus to set the status for test cases.

srTest::Dynamic

This example demonstrates how to use AddCase, testCase.AddAnnotation, and testAnnotation.AddComment for dynamic test case, and annotation creation within the context of one test method.

Run Tests

Now launch the test app (if you have not already) and execute the runner with the following commands:

Test Class tests:

stride --device="TCP:localhost:8000" --database="../out/TestApp.sidb" --run="s2_testclass::Basic::Exceptions; s2_testclass::Basic::Fixtures;  s2_testclass::Basic::Parameterized; s2_testclass::Basic::Simple" --output=TestClass.xml 

Note the command will produce distinct result files for the run (per the --output command above). Please use the result file to peruse the results by opening each in your browser.

Observations

This sample shows the techniques available for packaging and writing test units using classes. If you have a C++ capable compiler, we recommend that you use test classes to package your unit tests, even if your APIs under test are C only. Review the source code in the directory and follow the sample description.

The following are some test observations:

  • all of these example classes have been put into one or more namespaces. This is just for organization purposes, mainly to avoid name collisions when built along with lots of other test classes. Your test classes are not required to be in namespaces, but it can be helpful in avoiding collisions as the number of tests in your system grows.
  • we've documented our test classes and methods using doxygen style comments. This documentation is automatically extracted by our tools and added to the results report - more information about this feature is here
  • you can optionally write test classes that inherit from a base class that we've defined (stride::srTest). We recommend you start by writing your classes this way so that your classes will inherit some methods and members that make some custom reporting tasks simpler.
  • exceptions are generally handled by the STRIDE unit test harness, but can be disabled if your compiler does not support them (see s2_testclass_basic_exceptions_tests.h/cpp).
  • parameterized tests are supported by test classes as well. In these tests, simple constructor arguments can be passed during execution and are available at runtime to the test unit. The STRIDE infrastructure handles the passing of the arguments to the device and the construction of the test class with these arguments. Parameterization of test classes can be a powerful way to expand your test coverage with data driven test scenarios (varying the input to a single test class).