首页 > 代码库 > C++test 打桩

C++test 打桩

1       打桩
1.1.      编写源代码和测试代码
1.2.      编写桩函数
[Parasoft]-[显示视图]-[桩函数]
[选择原始的(函数)]-[右键创建用户桩函数…]
编写用户桩函数
2    源代码
#include <stdio.h>
int add(int a,int b)
{
      return a+b;
}
int compare(int a,int b)
{
       if (a>=b)
            return add(a,b);
       else
              return add(a,b);
}
void main()
{
       int a=10,b=5;
       int c=compare(a,b);
       printf("%d",c);
       scanf("%c",&c);
}
3    测试代码
#include "cpptest.h"
CPPTEST_CONTEXT("cpptest/cpptest/源文件/cpptest.cpp");
CPPTEST_TEST_SUITE_INCLUDED_TO("cpptest/cpptest/源文件/cpptest.cpp");
class TestSuite2 : public CppTest_TestSuite
{
   public:
       CPPTEST_TEST_SUITE(TestSuite2);
        CPPTEST_TEST(test_compare_1);
              CPPTEST_TEST(test_compare_2);
       CPPTEST_TEST_SUITE_END();
        void test_compare_1();
               void test_compare_2();
};
CPPTEST_TEST_SUITE_REGISTRATION(TestSuite2);
void TestSuite2::test_compare_1()
{
       int a=10,b=6;
       int c=compare(a,b);
       CPPTEST_ASSERT_INTEGER_EQUAL(4,c);
}

void TestSuite2::test_compare_2()
{
int a=10,b=6;
       int c=compare(a,b);
       CPPTEST_ASSERT_INTEGER_EQUAL(1,c);
}
4    桩代码
#include "cpptest.h"
int add (int a, int b) ;
int CppTest_Stub_add (int a, int b)
{
       // 根据不同的测试用例,例如test_compare_1,test_compare_2
       // 分别执行不同的桩函数分支,实现一个桩函数返回不同值。
       if (strcmp("test_compare_1", CppTest_GetCurrentTestCaseName())==0)
       {
         return 4;
       }
       else if (strcmp("test_compare_2", CppTest_GetCurrentTestCaseName())==0)
       {
       return 1;
       }
}

C++test 打桩