aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/junit.h
blob: 27b73013ca0fe733d6d06019a5a281705ff67745 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "registar.h"

#include <util/datetime/base.h>
#include <util/generic/maybe.h>
#include <util/system/tempfile.h>

#include <optional>

namespace NUnitTest {

extern const TString Y_UNITTEST_OUTPUT_CMDLINE_OPTION;
extern const TString Y_UNITTEST_TEST_FILTER_FILE_OPTION;

class TJUnitProcessor : public ITestSuiteProcessor {
    struct TFailure {
        TString Message;
        TString BackTrace;
    };

    struct TTestCase {
        TString Name;
        bool Success;
        TVector<TFailure> Failures;
        TString StdOut;
        TString StdErr;
        double DurationSecods = 0.0;

        size_t GetFailuresCount() const {
            return Failures.size();
        }
    };

    struct TTestSuite {
        TMap<TString, TTestCase> Cases;

        size_t GetTestsCount() const {
            return Cases.size();
        }

        size_t GetFailuresCount() const {
            size_t sum = 0;
            for (const auto& [name, testCase] : Cases) {
                sum += testCase.GetFailuresCount();
            }
            return sum;
        }

        double GetDurationSeconds() const {
            double sum = 0.0;
            for (const auto& [name, testCase] : Cases) {
                sum += testCase.DurationSecods;
            }
            return sum;
        }
    };

    // Holds a copy of TTest structure for current test
    class TCurrentTest {
    public:
        TCurrentTest(const TTest* test)
            : TestName(test->name)
            , Unit(*test->unit)
            , Test{&Unit, TestName.c_str()}
        {
        }

        operator const TTest*() const {
            return &Test;
        }

    private:
        TString TestName;
        TUnit Unit;
        TTest Test;
    };

    struct TOutputCapturer;

public:
    TJUnitProcessor(TString file, TString exec);
    ~TJUnitProcessor();

    void SetForkTestsParams(bool forkTests, bool isForked) override;

    void OnBeforeTest(const TTest* test) override;
    void OnError(const TError* descr) override;
    void OnFinish(const TFinish* descr) override;

private:
    TTestCase* GetTestCase(const TTest* test) {
        auto& suite = Suites[test->unit->name];
        return &suite.Cases[test->name];
    }

    void Save();

    size_t GetTestsCount() const {
        size_t sum = 0;
        for (const auto& [name, suite] : Suites) {
            sum += suite.GetTestsCount();
        }
        return sum;
    }

    size_t GetFailuresCount() const {
        size_t sum = 0;
        for (const auto& [name, suite] : Suites) {
            sum += suite.GetFailuresCount();
        }
        return sum;
    }

    void SerializeToFile();
    void MergeSubprocessReport();

    TString BuildFileName(size_t index, const TStringBuf extension) const;
    void MakeReportFileName();
    void MakeTmpFileNameForForkedTests();
    static void TransferFromCapturer(THolder<TJUnitProcessor::TOutputCapturer>& capturer, TString& out, IOutputStream& outStream);

    static void CaptureSignal(TJUnitProcessor* processor);
    static void UncaptureSignal();
    static void SignalHandler(int signal);

private:
    const TString FileName; // cmd line param
    const TString ExecName; // cmd line param
    TString ResultReportFileName;
    TMaybe<TTempFile> TmpReportFile;
    TMap<TString, TTestSuite> Suites;
    THolder<TOutputCapturer> StdErrCapturer;
    THolder<TOutputCapturer> StdOutCapturer;
    TInstant StartCurrentTestTime;
    void (*PrevAbortHandler)(int) = nullptr;
    void (*PrevSegvHandler)(int) = nullptr;
    std::optional<TCurrentTest> CurrentTest;
};

} // namespace NUnitTest