Visual Studio本机测试:在所有测试运行后写入文件 我使用VisualStudio 2015的CPUMPITestTestFrestsC++对我的C++代码运行了很多测试。但是,在所有测试运行之后,我想将一些结果写入一个文件。我通过TEST_CLASS_CLEANUP尝试了以下方法: TEST_CLASS_CLEANUP(CleanUp) { std::ofstream outFile("compare.html"); outFile << "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><table>"; //Write some stuff to outFile ... outFile << "</body></html>" << std::endl; outFile.close(); } 测试类清理(清理){ std::ofstream outFile(“compare.html”); outFile

Visual Studio本机测试:在所有测试运行后写入文件 我使用VisualStudio 2015的CPUMPITestTestFrestsC++对我的C++代码运行了很多测试。但是,在所有测试运行之后,我想将一些结果写入一个文件。我通过TEST_CLASS_CLEANUP尝试了以下方法: TEST_CLASS_CLEANUP(CleanUp) { std::ofstream outFile("compare.html"); outFile << "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><table>"; //Write some stuff to outFile ... outFile << "</body></html>" << std::endl; outFile.close(); } 测试类清理(清理){ std::ofstream outFile(“compare.html”); outFile,c++,visual-studio,unit-testing,visual-studio-2015,C++,Visual Studio,Unit Testing,Visual Studio 2015,是的,您可以。实际上,您的代码工作正常并创建了一个文件。请参见示例: TEST_CLASS(Example) { public: TEST_METHOD(ExampleTest) { // A test } private: TEST_CLASS_CLEANUP(ExampleCleanUp) { std::ofstream outFile("compare.html"); outFile <<

是的,您可以。实际上,您的代码工作正常并创建了一个文件。请参见示例:

TEST_CLASS(Example)
{
public:
    TEST_METHOD(ExampleTest)
    {
        // A test
    }

private:
    TEST_CLASS_CLEANUP(ExampleCleanUp) 
    {
        std::ofstream outFile("compare.html");
        outFile << "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><table>";

        outFile << "</body></html>" << std::endl;
        outFile.close();
    }
};
TEST_类(示例)
{
公众:
测试方法(示例测试)
{
//测验
}
私人:
测试类清理(示例清理)
{
std::ofstream outFile(“compare.html”);

outFile是的,您可以。实际上,您的代码工作正常并创建了一个文件。请参见示例:

TEST_CLASS(Example)
{
public:
    TEST_METHOD(ExampleTest)
    {
        // A test
    }

private:
    TEST_CLASS_CLEANUP(ExampleCleanUp) 
    {
        std::ofstream outFile("compare.html");
        outFile << "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><table>";

        outFile << "</body></html>" << std::endl;
        outFile.close();
    }
};
TEST_类(示例)
{
公众:
测试方法(示例测试)
{
//测验
}
私人:
测试类清理(示例清理)
{
std::ofstream outFile(“compare.html”);

outFile我知道可能已经太晚了,但是你的问题的答案是可以的,但是

Visual Studio 2017 CE中的以下实验:

TEST_CLASS_INITIALIZE(ClassInitialize)
    {
        Logger::WriteMessage("In Test Dir Listing Class Initialize");
        Logger::WriteMessage(std::experimental::filesystem::current_path().string().c_str());
        ... 
    }
给出了以下输出:

[dd.mm.2020 00:00:14 Informational] In Test Dir Listing Class Initialize [dd.mm.2020 00:00:14 Informational] C:\Users\user_name\source\repos\UnitTest1\Debug 导致:

[dd.mm.2020 00:00:15 Informational] In Test Dir Listing Class Cleanup [dd.mm.2020 00:00:15 Informational] F:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE [dd.mm.2020 00:00:15信息性]在测试目录中列出类清理 [dd.mm.2020 00:00:15信息]F:\Program Files(x86)\Microsoft Visual Studio\2017\Community\Common7\IDE 也就是说,清理类的工作目录不是通常可以看到的


因此,如果您想将数据写入某个特定文件,您应该安排适当的完整路径规范。或者在类初始化中保存工作路径,并在清理过程中使用它。

我知道这可能为时已晚,但您的问题的答案是可以的,但是

Visual Studio 2017 CE中的以下实验:

TEST_CLASS_INITIALIZE(ClassInitialize)
    {
        Logger::WriteMessage("In Test Dir Listing Class Initialize");
        Logger::WriteMessage(std::experimental::filesystem::current_path().string().c_str());
        ... 
    }
给出了以下输出:

[dd.mm.2020 00:00:14 Informational] In Test Dir Listing Class Initialize [dd.mm.2020 00:00:14 Informational] C:\Users\user_name\source\repos\UnitTest1\Debug 导致:

[dd.mm.2020 00:00:15 Informational] In Test Dir Listing Class Cleanup [dd.mm.2020 00:00:15 Informational] F:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE [dd.mm.2020 00:00:15信息性]在测试目录中列出类清理 [dd.mm.2020 00:00:15信息]F:\Program Files(x86)\Microsoft Visual Studio\2017\Community\Common7\IDE 也就是说,清理类的工作目录不是通常可以看到的


因此,如果您想将数据写入某个特定文件,您应该安排适当的完整路径规范。或者在类初始化中保存工作路径,并在清理过程中使用它。

听起来您应该尝试类似于测试的方法_CLEANUP@ChristopherGall就像我写的,类的清理方法,即TEST_class_CLEANUP不起作用。我还尝试了模块清理测试\u模块清理,该测试也不起作用。剩余的清理测试\u方法\u清理工作正常,但在每次测试后都会运行。不存在简单的测试\u清理。是否有名为“清理”的测试类在您的项目中?@ChristopherGallé我的测试类默认命名为UnitTest1,CleanUp是CleanUp方法的名称,请参见:听起来您应该尝试类似test的东西_CLEANUP@ChristopherGall就像我写的,类的清理方法,即TEST_class_cleanup不起作用。我还尝试了模块清理TEST_module_cleanup也不起作用。剩余的清理测试\u方法\u清理工作正常,但会在每次测试后运行。不存在简单的测试\u清理。是否有名为“清理”的测试类在您的项目中?@ChristopherGallé我的测试类默认命名为UnitTest1,CleanUp是清理方法的名称,请参见此处:我刚刚尝试了您的代码示例,并将其复制到了一个新的本机单元测试中。不幸的是,结果保持不变,并且在我的示例中没有显示任何文件。但很高兴知道,即使在我的情况并非如此。不幸的是,我不知道什么会影响文件编写。我只是尝试了您的代码示例,并将其复制到一个新的本机单元测试中。不幸的是,结果保持不变,在我的情况下,没有文件出现。但很高兴知道代码应该可以工作,即使在我的情况下,它不能工作。不幸的是,我不知道什么会影响文件写入。