CPPUNIT Source Code Interpretation (6)

zhaozj2021-02-17  58

[Declaration] If you need to copy, spread, please attach this statement, thank you. Original source: http://morningspace.51.net/ ,moyingzz@etang.com

Extension (Extension)

In CPPUNIT, in addition to providing basic unit testing, there is also a lot of extended tests, such as repeatTest, formal test (ORTHODOXTEST), which are all included in Extension.

[TestDecorator]

Related Documents: TestDecorator.h

It provides a method that can expand the Test class without subclass of Test classes. We can derive TestDecorator and use it to pack TEST. In fact, this method is an application of Decorator Pattern, which is described below in GOF: Dynamically add some additional duties to an object. In addition, it is more flexible than generated subclasses.

TestDecorator maintains a pointer to the Test instance and sets in the CTOR. However, the life of this example, TestDecorator is not asking:

protected:

Test * m_test;

Then four public functions, the interfaces are completely consistent with TEST: Void Run (TestResult * Result);

INT COUNTTESTCASES () Const;

Std :: string getname () const;

Std: string toString () const;

The implementation of the function is a simple call m_test's correspondence interface: Inline int testDecorator :: counttestcases () Const

{RETURN M_TEST-> countTestCases ();

Inline Void TestDecorator :: Run (TestResult * Result)

{m_test-> run (result);}

Inline std :: string testdecorator :: toString () const

{RETURN M_TEST-> TOSTRING ();

Inline std :: string testdecorator :: getname () const

{RETURN M_TEST-> getName ();

These features will be extended in the derived class of TestDecorator.

[RepeatedTest]

Related Documents: RepeatedTest.h, RepeatedTest.cpp

Delivered from TestDecorator, its function is the number of times the test repeatedly (similar to a certain intensity test). The private member variable m_timeSrepeat records repetition:

Private:

Const int m_timesrepeat;

This value is set in the CTOR: REPEATEDTEST (Test * Test, Int TimeSrepeat):

TestDecorator (TEST),

M_TimeSrepeat {}

The TEST parameters here are the tests to be executed, which may be a test case or a test package. The subsequent version of the function counttestcases, run and torstring: // Returns the total number of test cases / / total number = actual number * Repeat

INT RepeatedTest :: CountTestCases () Const

{

Return TestDecorator :: counttestcases () * m_timesis

}

Std :: string repeatedtest :: toString () const

{

Return TestDecorator :: TOSTRING () "(REPEATED)

}

// run test

// Repeat the RUN of the base class and call the M_Test's RUN method in the base class.

Void RepeatedTest :: Run (TestResult * Result)

{

For (int N = 0; n

{

if (Result-> ShouldStop ())

Break;

TestDecorator :: Run (result);

}

}

[ORTHODOX]

Related documents: Orthodox.h

This class implements the function of formal testing. It is derived from TestCase, a template class, has a type of parameter ClassUndertest, which is a test that will run. The so-called formal test is to perform a simple test of a set of simple tests (ie, classundertest) to ensure that it has at least the following basic operations:

Default ctor operator == and operator! = assignment (ie Operator =) Operator! Safe Passage (ie Copy CTOR)

If any of the items do not pass test, the template class will not instantiate. Otherwise, it will check whether the semantics of these operations are correct after instantiation. When you need to confirm that a group of pending classes has the same performance, it is very useful to use the templated test case, ORTHODOX is a good example. I can imagine that in practice, we can also follow the Orthodox approach to "extend" CPPUnit to accommodate your specific environment. The following code demonstrates how to add a formal test of a plurality of classes to the test package: testsuite * suiteoftests = new testsuite;

SuiteOftests-> Addtest (New ComplexNumbertest ("TestAdd");

SuiteOftests-> AddTest (New TestCaller > ()); // is very simple

Take a look at ORTHODOX: Template Class Orthodox: Public Testcase

{

PUBLIC:

ORTHODOX (): Testcase ("Orthodox") {}

protected:

ClassunderTest Call (ClassunderTest Object);

Void runtest ();

}

The only thing that needs to be explained is the runtest method. How to check if ClassunderTest meets the requirements: Template Void Orthodox :: runtest ()

{

/ / Make sure the default ctor is defined, otherwise it cannot be compiled

ClassunderTest A, B, C;

/ / Make sure that Operator == is defined, otherwise it is not possible to compile

/ / Take the semantics of Operator ==

CPPUNIT_ASSERT (a == b);

/ / Make sure Operator!, Operator = and Operator! = Is defined

/ / Otherwise, it is not possible to compile

/ / Check the semantics of Operator! =

B. Operator = (a.operator! ());

CPPUNIT_ASSERT (a! = B);

// Check the semantics of Operator! And Operator ==

B = !! a;

CPPUNIT_ASSERT (a == b);

B =! A;

// The following checks if the COPY CTOR is defined and its semantic is correct or not

C = a;

CPPUNIT_ASSERT (C == Call (a));

C = B;

CPPUNIT_ASSERT (C == Call (b));

}

The Call here is an auxiliary function, "forcing" the compiler calls the Copy CTOR to check Safe Passage: Template

ClassunderTest Orthodox :: Call (classundertest Object)

{

Return Object;

}

All the mystery is in the above line of code.

[TestSetup]

Related Documents: TestSetup.h, TestSetup.cpp

Same as TestDecorator, which enables the test class with the features of Setup and Teardown. See the TestFixTure of the Core section about these two features.

This class defines the virtual functions of the two protected properties to provide born class coverage:

protected:

Virtual void setup ();

Virtual void teardown ();

In addition, it is a subclass of RUN methods: void test setup :: run (testresult * result)

{

setup ();

TestDecorator :: Run (result);

TEARDOWN ();

}

[postscript]

As I said in the article "CPPUnit Source Code (1)", this series of articles is selected from the "CPPUnit Source Interpretation", and I think it is the main part of the chapter. I plan to do this article as the end of this series of articles, but friends who are still interested in this series will be able to find this series of articles on my homepage: http://morningspace.51.net/Resource/ CPPUNIT / CPPUNIT_ANNO.HTML.

Of course, the so-called mainly is only subjective division of individuals. For example, another important part of CPPUnit - the result is output, which is not mentioned here, it is the land of Observer Pattern. In addition, CPPUnit's class factory realization mechanism is also very distinctive. In these issues, you will be able to find it in the full version.

Doing this, it is for the following considerations: The content of the article is out of the source code for personal reading notes (originally written to himself :), and because of the time relationship, I can't do it very delicate (like just like Some people complain that there is no picture, class diagram, etc. :). I have been inevitably feeling a bit like a record of water account for a long time. In this way, it is better to let friends who don't mind flowing, they will come to the website to see the following content, and the mind does not need to continue to look down :) Rea have again, this article is the source code notes, if you want to know about the use of CPPUnit, please refer to Other literature, according to my knowledge of 9CBS, there is an entry in the center of the documentation :)

- MORNING

转载请注明原文地址:https://www.9cbs.com/read-29136.html

New Post(0)