aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/junit.h
blob: 2eca875310d65328aae9193cc41770f320ea38ec (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
#include "registar.h"

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

namespace NUnitTest {

extern const TString Y_UNITTEST_OUTPUT_CMDLINE_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;
        }
    };

    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();

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;
};

} // namespace NUnitTest