mirror of
https://gitlab.kitware.com/cmake/cmake.git
synced 2026-09-25 04:09:36 +03:00
When ctest repeats tests with its --repeat option, it repeats each test on its own. A fixture's setup and cleanup tests therefore run all of their repetitions back to back, and the tests they bracket repeat inside a single setup/cleanup pair: setup -> setup -> test -> test -> cleanup -> cleanup Add a FIXTURE_REPEAT_MODE test property to select how a fixture behaves when its tests are repeated: * AROUND_ALL_REPEATS: the fixture runs once, around all repetitions of the tests requiring it. * AROUND_EACH_REPEAT: the fixture and the tests requiring it repeat together, so every repetition gets a fresh setup and its own cleanup. * EACH_TEST_SEPARATELY: every test repeats on its own, as before. The property describes the fixture rather than the test carrying it, so setting it on any one of a fixture's setup or cleanup tests is enough. In AROUND_EACH_REPEAT mode the tests of a fixture form a repeat group that ctest re-queues as a whole once every test in it has finished. The --repeat condition then applies to the group the way it applies to an individual test: until-fail repeats while the whole group passes, until-pass repeats while any of it does not, and after-timeout repeats while any of it times out. Fixtures that share a test repeat together, so a test requiring two of them still runs once per repetition. A group is recorded the way a repeating test is: only once it stops repeating, and with the results of its last repetition. A group that until-pass makes pass therefore reports a pass rather than the failure that made it repeat, a test that DEPENDS on one of the group's tests waits for the last repetition rather than the first, and `ctest -F` resumes an interrupted group by running it again from the beginning. Fixtures that repeat together have to agree on the mode: a test cannot repeat with one fixture but not with another it takes part in, and a fixture whose setup and cleanup tests disagree has no coherent behavior. Report an error and run nothing in those cases rather than pick an order in which a test repeats after a fixture it requires has been cleaned up. Add policy CMP0224 to select AROUND_EACH_REPEAT as the default for fixtures whose setup and cleanup tests choose no mode themselves. Record the mode the policy chose in the generated test file under its own _CMAKE_DEFAULT_FIXTURE_REPEAT_MODE keyword, so that ctest reads a mode rather than the policy settings behind it, and so that a mode requested on one of a fixture's tests wins over the default recorded for its siblings. Only NEW needs recording: with nothing recorded, ctest already uses the behavior of CMake 4.4 and below. Fixtures are common, and the choice of mode matters only to those who run ctest --repeat, so warn about the unset policy only when the CMAKE_POLICY_WARNING_CMP0224 variable asks for it. discover_tests() and gtest_discover_tests() create their tests while ctest runs or at build time, too late for the policy to reach them, so carry the setting in effect at their call sites through to the tests they create. Report the repetition a grouped test belongs to in the "(run N/M)" suffix of its "Start" line, as ctest already does for a test repeating on its own. Co-authored-by: Tyler Yankee <tyler.yankee@kitware.com> Fixes: #21438
183 lines
5.1 KiB
C++
183 lines
5.1 KiB
C++
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
file LICENSE.rst or https://cmake.org/licensing for details. */
|
|
#pragma once
|
|
|
|
#include "cmConfigure.h" // IWYU pragma: keep
|
|
|
|
#include <cstddef>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <cm/optional>
|
|
|
|
#include "cmCTest.h"
|
|
#include "cmCTestMultiProcessHandler.h"
|
|
#include "cmCTestTestHandler.h"
|
|
#include "cmProcess.h"
|
|
|
|
class cmEnvironment;
|
|
|
|
/** \class cmRunTest
|
|
* \brief represents a single test to be run
|
|
*
|
|
* cmRunTest contains the information related to running a single test
|
|
*/
|
|
class cmCTestRunTest
|
|
{
|
|
public:
|
|
explicit cmCTestRunTest(cmCTestMultiProcessHandler& multiHandler, int index);
|
|
|
|
// Report this run as run `number` of `count` in the test's "(run N/M)"
|
|
// suffix. A test repeating on its own starts at run 1 and counts up
|
|
// itself; a test repeated by its fixture is told which run it is.
|
|
void SetRunNumber(int number, int count)
|
|
{
|
|
this->RunNumber = number;
|
|
this->RunCount = count;
|
|
}
|
|
|
|
void SetRepeatMode(cmCTest::Repeat r) { this->RepeatMode = r; }
|
|
|
|
cmCTestTestHandler::cmCTestTestProperties* GetTestProperties()
|
|
{
|
|
return this->TestProperties;
|
|
}
|
|
|
|
int GetIndex() { return this->Index; }
|
|
|
|
void AddFailedDependency(std::string const& failedTest)
|
|
{
|
|
this->FailedDependencies.insert(failedTest);
|
|
}
|
|
|
|
std::string GetProcessOutput() { return this->ProcessOutput; }
|
|
|
|
cmCTestTestHandler::cmCTestTestResult GetTestResults()
|
|
{
|
|
return this->TestResult;
|
|
}
|
|
|
|
// Read and store output. Returns true if it must be called again.
|
|
void CheckOutput(std::string const& line);
|
|
|
|
static void StartTest(std::unique_ptr<cmCTestRunTest> runner,
|
|
size_t completed, size_t total);
|
|
static bool StartAgain(std::unique_ptr<cmCTestRunTest> runner,
|
|
size_t completed);
|
|
|
|
static void StartFailure(std::unique_ptr<cmCTestRunTest> runner,
|
|
size_t total, std::string const& output,
|
|
std::string const& detail);
|
|
|
|
struct EndTestResult
|
|
{
|
|
bool Passed = false;
|
|
bool StopTimePassed = false;
|
|
int TestStatus = cmCTestTestHandler::NOT_RUN;
|
|
};
|
|
|
|
// launch the test process, return whether it started correctly
|
|
bool StartTest(size_t completed, size_t total);
|
|
// capture and report the test results
|
|
EndTestResult EndTest(size_t completed, size_t total, bool started);
|
|
// Called by ctest -N to log the command string
|
|
void ComputeArguments();
|
|
|
|
void ComputeWeightedCost();
|
|
|
|
void StartFailure(size_t total, std::string const& output,
|
|
std::string const& detail);
|
|
|
|
cmCTest* GetCTest() const { return this->CTest; }
|
|
|
|
std::string& GetActualCommand() { return this->ActualCommand; }
|
|
|
|
std::vector<std::string> const& GetArguments() { return this->Arguments; }
|
|
|
|
void FinalizeTest(bool started = true);
|
|
|
|
void SetUseAllocatedResources(bool use)
|
|
{
|
|
this->UseAllocatedResources = use;
|
|
}
|
|
void SetAllocatedResources(
|
|
std::vector<std::map<
|
|
std::string,
|
|
std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>> const&
|
|
resources)
|
|
{
|
|
this->AllocatedResources = resources;
|
|
}
|
|
|
|
private:
|
|
struct TimeValue
|
|
{
|
|
long tv_sec;
|
|
long tv_usec;
|
|
};
|
|
|
|
struct ProcessMetrics
|
|
{
|
|
TimeValue ru_utime{};
|
|
TimeValue ru_stime{};
|
|
long ru_maxrss = 0;
|
|
};
|
|
|
|
bool NeedsToRepeat();
|
|
void ParseOutputForMeasurements();
|
|
void ExeNotFound(std::string exe);
|
|
bool ForkProcess();
|
|
void WriteLogOutputTop(size_t completed, size_t total);
|
|
// Run post processing of the process output for MemCheck
|
|
void MemCheckPostProcess();
|
|
std::string GenerateLLVMPath(std::string fileString);
|
|
std::string GetTestMetricsFile() const;
|
|
void CollectLLVMCoverage();
|
|
void SetupResourcesEnvironment(cmEnvironment& env);
|
|
|
|
// Returns "completed/total Test #Index: "
|
|
std::string GetTestPrefix(size_t completed, size_t total) const;
|
|
|
|
cmCTestMultiProcessHandler& MultiTestHandler;
|
|
int Index;
|
|
cmCTest* CTest;
|
|
cmCTestTestHandler* TestHandler;
|
|
cmCTestTestHandler::cmCTestTestProperties* TestProperties;
|
|
|
|
std::unique_ptr<cmProcess> TestProcess;
|
|
std::string ProcessOutput;
|
|
cmCTestTestHandler::cmCTestTestResult TestResult;
|
|
std::set<std::string> FailedDependencies;
|
|
std::string StartTime;
|
|
std::string ActualCommand;
|
|
std::vector<std::string> Arguments;
|
|
std::string InstrumentationCommand;
|
|
std::vector<std::string> InstrumentationArguments;
|
|
cm::optional<ProcessMetrics> ProcessResourceUsage;
|
|
bool UseAllocatedResources = false;
|
|
std::vector<std::map<
|
|
std::string, std::vector<cmCTestMultiProcessHandler::ResourceAllocation>>>
|
|
AllocatedResources;
|
|
// Never unless this test repeats itself. A test repeated by its fixture
|
|
// leaves this Never; the fixture decides when to repeat.
|
|
cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
|
|
int RunNumber = 1; // which run of the test this is
|
|
int RunCount = 1; // how many runs it may be given
|
|
bool RunAgain = false;
|
|
bool UseLLVMCov = false;
|
|
size_t TotalNumberOfTests;
|
|
};
|
|
|
|
inline int getNumWidth(size_t n)
|
|
{
|
|
int w = 1;
|
|
while (n >= 10) {
|
|
n /= 10;
|
|
++w;
|
|
}
|
|
return w;
|
|
}
|