简体   繁体   English

单元测试-如果在生产代码中仅调用一次函数,则在两个测试用例中调用函数

[英]Unit Test - call a function in two test cases if the function is called only once in productive code

maybe there is someone who has experience with Unit Testing with cpputest . 也许有人在cpputest进行单元测试方面有经验。

I have something like this: 我有这样的事情:

Source code Under Test: 测试中的源代码:

main_function()
{
   static int8 is_functioncalled = 1;

   if (is_functioncalled){
   my_local_function();
   is_functioncalled = 0
   }

UNIT Test Environment: UNIT测试环境:

TEST(TESTGROUP,TEST_CASE1){

   //Some Unit Test checks of my_local_function()

   main_function();
}

TEST(TESTGROUP,TEST_CASE2){

   //Some other Unit Test stuff

   main_function();        // --> my_local_function() will not be called in this test case because it's called already before

} }

I need in TEST_CASE2 the function my_local_function() to be called again. 我需要在TEST_CASE2中再次调用函数my_local_function() This function is indirectly called through the public interface main_function() which can be called directly within the Unit Test. 通过公共接口main_function()间接调用此函数,可以在单元测试中直接调用该接口。 Does anybody have an idea how to do this in generally or in cpputest environment ? 有没有人知道如何在一般或cpputest环境中执行此操作

Try to override setup() method of test group - it will be called before each test. 尝试覆盖测试组的setup()方法-它将在每次测试之前调用。 You could reset is_functioncalled flag there if you would put it in global scope, something like this: 如果将其放置在全局范围内,则可以在其中重置is_functioncalled标志,如下所示:

static int8 is_functioncalled = 1;
main_function()
{
   if (is_functioncalled){
   my_local_function();
   is_functioncalled = 0
   }
}

// //

extern int8 is_functioncalled; // If its in global scope in other source file

TEST_GROUP(TESTGROUP)
{
   void setup()
   {
      is_functioncalled = 1;
   }
}

Try https://cpputest.github.io/manual.html - there's all you need to know. 尝试https://cpputest.github.io/manual.html-您需要了解的所有信息。

You may add a define into your code, modifying behaviour if it is under testing: 您可以在代码中添加定义,如果正在测试中,则可以修改行为:

    main_function()
    {
        static int8 is_functioncalled = 1;
    #ifdef UNITTEST
        is_functioncalled = 1;
    #endif
        if (is_functioncalled){
            my_local_function();
            is_functioncalled = 0
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM