微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CppUnitTestFramework:测试方法失败,堆栈跟踪在方法结束时列出行号,调试测试通过

如何解决CppUnitTestFramework:测试方法失败,堆栈跟踪在方法结束时列出行号,调试测试通过

我知道,我知道 - 问题标题到处都是。但是,我不确定这里可能是什么问题导致了我所看到的。

我在 Project 类中有以下方法正在接受单元测试:

bool Project::DetermineID(std::string configFile,std::string& ID)
{
    std::ifstream config;
    config.open(configFile);

    if (!config.is_open()) {
        WARNING << "Failed to open the configuration file for processing ID at: " << configFile;
        return false;
    }

    std::string line = "";
    ID = "";

    bool isConfigurationSection = false;
    bool isConfiguration = false;

    std::string tempID = "";

    while (std::getline(config,line))
    {
        std::transform(line.begin(),line.end(),line.begin(),::toupper); // transform the line to all capital letters
        boost::trim(line);

        if ((line.find("IDENTIFICATIONS") != std::string::npos) && (!isConfigurationSection)) {

            // remove the "IDENTIFICATIONS" part from the current line we're working with
            std::size_t idStartPos = line.find("IDENTIFICATIONS");
            line = line.substr(idStartPos + strlen("IDENTIFICATIONS"),line.length() - idStartPos - strlen("IDENTIFICATIONS"));
            boost::trim(line);

            isConfigurationSection = true;
        }

        if ((line.find('{') != std::string::npos) && isConfigurationSection) {

            std::size_t bracketPos = line.find('{');

            // we are working within the ids configuration section
            // determine if this is the first character of the line,or if there is an ID that precedes the { 

            if (bracketPos == 0) {
                // is the first char
                // remove the bracket and keep processing

                line = line.substr(1,line.length() - 1);
                boost::trim(line);
            }
            else {
                // the text before { is a temp ID

                tempID = line.substr(0,bracketPos - 1);
                isConfiguration = true;

                line = line.substr(bracketPos,line.length() - bracketPos);
                boost::trim(line);
            }
        }

        if ((line.find("PORT") != std::string::npos) && isConfiguration) {

            std::size_t indexOfEqualSign = line.find('=');

            if (indexOfEqualSign == std::string::npos) {
                WARNING << "Unable to determine the port # assigned to " << tempID;
            }
            else {
                std::string portString = "";
                portString = line.substr(indexOfEqualSign + 1,line.length() - indexOfEqualSign - 1);
                boost::trim(portString);

                // confirm that the obtained port string is not an empty value

                if (portString.empty()) {
                    WARNING << "Failed to obtain the \"Port\" value that is set to " << tempID;
                }
                else {

                    // attempt to convert the string to int

                    int workingPortNum = 0;

                    try {
                        workingPortNum = std::stoi(portString);
                    }
                    catch (...) {
                        WARNING << "Failed to convert the obtained \"Port\" value that is set to " << tempID;
                    }

                    if (workingPortNum != 0) {
                        // check if this port # is the same port # we are publishing data on

                        if (workingPortNum == this->port) {
                            ID = tempID;
                            break;
                        }
                    }
                }
            }
        }
    }

    config.close();

    if (ID.empty())
        return false;
    else
        return true;
}

方法的目标是基于匹配应用程序将数据发布到的端口号来解析 ID 部分的任何文本文件

文件格式如下:

Idenntifications {
  ID {
    port = 1001
  }
}

一个单独的 Visual Studio 项目中对各种方法进行单元测试,包括这个 Project::DetermineID 方法

#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)

TEST_CLASS(ProjectUnitTests) {
    Project* parser;
    std::string projectDirectory;

    TEST_METHOD_INITIALIZE(ProjectUnitTestinitialization) {
        projectDirectory = EXPAND(UNITTESTPRJ);
        projectDirectory.erase(0,1);
        projectDirectory.erase(projectDirectory.size() - 2);

        parser = Project::getClass(); // singleton method getter/initializer
    }

    // Other test methods are present and pass/fail accordingly

    TEST_METHOD(DetermineID) {
        std::string ID = "";

        bool x = parser ->DetermineAdapterID(projectDirectory + "normal.cfg",ID);
        Assert::IsTrue(x);
    }
};

现在,当我运行测试时,DetermineID 失败并且堆栈跟踪状态:

   DetermineID
   Source: Project Tests.cpp line 86
   Duration: 2 sec

  Message: 
    Assert Failed
  Stack Trace: 
    ProjectUnitTests::DetermineID() line 91

现在,在我的测试 .cpp 文件中,TEST_METHOD(DetermineID) { 位于第 86 行。但该方法} 位于第 91 行,如堆栈跟踪所示。

并且,在调试的时候,单元测试通过了,因为xTEST_METHOD的返回是true。 只有在单独运行测试或运行所有测试时,该测试方法才会失败。

一些可能相关的注释:

  • 这是一个单线程应用程序,没有安排任何任务(假设不需要担心竞争条件)
  • Project 类中还​​有另一个方法,它也处理具有 std::ifstream文件,与此方法相同
  • 是的,this->port一个赋值

因此,我的问题是:

  1. 为什么堆栈跟踪引用了测试方法的右括号,而不是方法中可能失败的单个 Assert
  2. 如何让单元测试在运行时通过? (因为它目前只在调试期间出现,我可以确认 xtrue)。
  3. 如果问题是其他测试方法可能正在访问“normal.cfg”文件的竞争条件,为什么即使单独运行该方法,测试方法也会失败?

非常感谢这里的任何支持/帮助。谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。