Unit Tests Manual

内容表

关于

maxon::UnitTestInterface provides an unified interface for any kind of unit test. Such an unit test should verify the correct functionality of every component of a program. Unit tests implemented this way are automatically executed when Cinema 4D starts and unit tests are enabled.

Implementation

Custom unit test are created by implementing a custom class based on maxon::UnitTestComponent . This custom class must implement maxon::UnitTestInterface::Run() which contains the unit test code.

The result of a test can be printed to the console using a standard format:

A (long running) unit test may be cancelled from the outside:

注意
To measure time for speed tests one can use maxon::TimeValue .
// This example shows a unit test that checks a custom class.
class SimpleMathsUnitTest : public maxon::UnitTestComponent <SimpleMathsUnitTest> { MAXON_COMPONENT ();
public : maxon::Result<void> Run() { // test constructor / SimpleMaths::GetNumber() MAXON_SCOPE { const maxon::Int number = 5; const SimpleMaths baseTest(number); const maxon::Result<void> baseRes = baseTest.GetNumber() == number ? maxon::OK : maxon::UnitTestError( MAXON_SOURCE_LOCATION , "GetNumber() failed" _s); self .AddResult( "GetNumber() test" _s, baseRes);

// if GetNumber() fails the rest of the unit tests cannot succeed if (baseRes. GetError ()) return baseRes; }

// test SimpleMaths::AddNumber() MAXON_SCOPE { SimpleMaths addTest(99); addTest.AddNumber(1); const maxon::Result<void> addTestRes = addTest.GetNumber() == 100 ? maxon::OK : maxon::UnitTestError( MAXON_SOURCE_LOCATION , "AddNumber() failed" _s); self .AddResult( "AddNumber() test" _s, addTestRes); }

// test SimpleMaths::SubstractNumber() MAXON_SCOPE { SimpleMaths subTest(100); subTest.SubstractNumber(1); const maxon::Result<void> subTestRes = subTest.GetNumber() == 99 ? maxon::OK : maxon::UnitTestError( MAXON_SOURCE_LOCATION , "SubstractNumber() failed" _s); self .AddResult( "SubstractNumber() test" _s, subTestRes); } return maxon::OK ; } };

The result of this unit test will be printed to the console:

========================================== @Running unit test 1 / 1: net.example.unittest.simplemaths ========================================== Unittest "net.example.unittest.simplemaths" OK : GetNumber() test Unittest "net.example.unittest.simplemaths" OK : AddNumber() test Unittest "net.example.unittest.simplemaths" OK : SubstractNumber() test Duration: 0.5 ms ========================================== @ScriptRef End

配准

Different kinds of unit tests can be registered at these registries:

To run all unit tests set the command line argument to "*" like g_runUnitTests=* . To run a specific test one can define a filter like g_runUnitTests=*simplemaths* .

// This example registers the given unit test at the registry maxon::UnitTestClasses. MAXON_COMPONENT_CLASS_REGISTER (SimpleMathsUnitTest, maxon::UnitTestClasses, "net.maxonexample.unittest.simplemaths" );

延伸阅读

MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
maxon::OK
return OK
定义: apibase.h:2532
MAXON_COMPONENT_CLASS_REGISTER
#define MAXON_COMPONENT_CLASS_REGISTER(C,...)
定义: objectbase.h:2233
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
maxon::Result< void >
OK
OK
Ok.
定义: ge_prepass.h:2
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
maxon::UnitTestComponent
定义: unittest.h:123
maxon::Result::GetError
const Error & GetError() const
定义: resultbase.h:1026
MAXON_SCOPE
#define MAXON_SCOPE
定义: apibase.h:2645

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1