1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-22 16:05:58 +01:00

Merge pull request #762 from koy-rehme-bae/fixtures_command_line_arguments

New command line options for fixtures
This commit is contained in:
Mark VanderVoord
2025-01-21 14:43:33 -05:00
committed by GitHub
4 changed files with 102 additions and 14 deletions

View File

@@ -46,21 +46,25 @@ int UnityMain(int argc, const char* argv[], void (*runAllTests)(void))
return (int)Unity.TestFailures;
}
static int selected(const char* filter, const char* name)
static int selected(const char* filter, const char* select, const char* name)
{
if (filter == 0)
if (filter == 0 && select == 0)
return 1;
return strstr(name, filter) ? 1 : 0;
if (filter && strstr(name, filter))
return 1;
if (select && strcmp(name, select) == 0)
return 1;
return 0;
}
static int testSelected(const char* test)
{
return selected(UnityFixture.NameFilter, test);
return selected(UnityFixture.NameFilter, UnityFixture.Name, test);
}
static int groupSelected(const char* group)
{
return selected(UnityFixture.GroupFilter, group);
return selected(UnityFixture.GroupFilter, UnityFixture.Group, group);
}
void UnityTestRunner(unityfunction* setup,
@@ -96,17 +100,20 @@ void UnityTestRunner(unityfunction* setup,
Unity.NumberOfTests++;
UnityPointer_Init();
UNITY_EXEC_TIME_START();
if (!UnityFixture.DryRun) {
UNITY_EXEC_TIME_START();
if (TEST_PROTECT())
{
setup();
testBody();
}
if (TEST_PROTECT())
{
teardown();
if (TEST_PROTECT())
{
setup();
testBody();
}
if (TEST_PROTECT())
{
teardown();
}
}
if (TEST_PROTECT())
{
UnityPointer_UndoAllSets();
@@ -183,8 +190,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
int i;
UnityFixture.Verbose = 0;
UnityFixture.Silent = 0;
UnityFixture.DryRun = 0;
UnityFixture.GroupFilter = 0;
UnityFixture.Group = 0;
UnityFixture.NameFilter = 0;
UnityFixture.Name = 0;
UnityFixture.RepeatCount = 1;
if (argc == 1)
@@ -207,10 +217,16 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UNITY_PRINT_EOL();
UnityPrint(" -s Silent mode: minimal output showing only test failures");
UNITY_PRINT_EOL();
UnityPrint(" -d Dry run all tests");
UNITY_PRINT_EOL();
UnityPrint(" -g NAME Only run tests in groups that contain the string NAME");
UNITY_PRINT_EOL();
UnityPrint(" -G NAME Only run tests in groups named NAME");
UNITY_PRINT_EOL();
UnityPrint(" -n NAME Only run tests whose name contains the string NAME");
UNITY_PRINT_EOL();
UnityPrint(" -N NAME Only run tests named NAME");
UNITY_PRINT_EOL();
UnityPrint(" -r NUMBER Repeatedly run all tests NUMBER times");
UNITY_PRINT_EOL();
UnityPrint(" -h, --help Display this help message");
@@ -237,6 +253,11 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.Silent = 1;
i++;
}
else if (strcmp(argv[i], "-d") == 0)
{
UnityFixture.DryRun = 1;
i++;
}
else if (strcmp(argv[i], "-g") == 0)
{
i++;
@@ -245,6 +266,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.GroupFilter = argv[i];
i++;
}
else if (strcmp(argv[i], "-G") == 0)
{
i++;
if (i >= argc)
return 1;
UnityFixture.Group= argv[i];
i++;
}
else if (strcmp(argv[i], "-n") == 0)
{
i++;
@@ -253,6 +282,14 @@ int UnityGetCommandLineOptions(int argc, const char* argv[])
UnityFixture.NameFilter = argv[i];
i++;
}
else if (strcmp(argv[i], "-N") == 0)
{
i++;
if (i >= argc)
return 1;
UnityFixture.Name = argv[i];
i++;
}
else if (strcmp(argv[i], "-r") == 0)
{
UnityFixture.RepeatCount = 2;

View File

@@ -17,9 +17,12 @@ struct UNITY_FIXTURE_T
{
int Verbose;
int Silent;
int DryRun;
unsigned int RepeatCount;
const char* NameFilter;
const char* Name;
const char* GroupFilter;
const char* Group;
};
extern struct UNITY_FIXTURE_T UnityFixture;

View File

@@ -90,23 +90,32 @@ TEST_GROUP(UnityCommandOptions);
static int savedVerbose;
static unsigned int savedRepeat;
static int savedDryRun;
static const char* savedName;
static const char* savedGroup;
static const char* savedNameExact;
static const char* savedGroupExact;
TEST_SETUP(UnityCommandOptions)
{
savedVerbose = UnityFixture.Verbose;
savedRepeat = UnityFixture.RepeatCount;
savedDryRun = UnityFixture.DryRun;
savedName = UnityFixture.NameFilter;
savedGroup = UnityFixture.GroupFilter;
savedNameExact = UnityFixture.Name;
savedGroupExact = UnityFixture.Group;
}
TEST_TEAR_DOWN(UnityCommandOptions)
{
UnityFixture.Verbose = savedVerbose;
UnityFixture.RepeatCount= savedRepeat;
UnityFixture.DryRun = savedDryRun;
UnityFixture.NameFilter = savedName;
UnityFixture.GroupFilter = savedGroup;
UnityFixture.Name= savedNameExact;
UnityFixture.Group= savedGroup;
}
@@ -118,8 +127,11 @@ TEST(UnityCommandOptions, DefaultOptions)
{
UnityGetCommandLineOptions(1, noOptions);
TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
TEST_ASSERT_EQUAL(0, UnityFixture.DryRun);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Group);
TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.Name);
TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
}
@@ -134,6 +146,17 @@ TEST(UnityCommandOptions, OptionVerbose)
TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
}
static const char* dryRun[] = {
"testrunner.exe",
"-d"
};
TEST(UnityCommandOptions, OptionDryRun)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, dryRun));
TEST_ASSERT_EQUAL(1, UnityFixture.DryRun);
}
static const char* group[] = {
"testrunner.exe",
"-g", "groupname"
@@ -156,6 +179,28 @@ TEST(UnityCommandOptions, OptionSelectTestByName)
STRCMP_EQUAL("testname", UnityFixture.NameFilter);
}
static const char* groupExact[] = {
"testrunner.exe",
"-G", "groupname"
};
TEST(UnityCommandOptions, OptionSelectTestByGroupExact)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, groupExact));
STRCMP_EQUAL("groupname", UnityFixture.Group);
}
static const char* nameExact[] = {
"testrunner.exe",
"-N", "testname"
};
TEST(UnityCommandOptions, OptionSelectTestByNameExact)
{
TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, nameExact));
STRCMP_EQUAL("testname", UnityFixture.Name);
}
static const char* repeat[] = {
"testrunner.exe",
"-r", "99"

View File

@@ -19,8 +19,11 @@ TEST_GROUP_RUNNER(UnityCommandOptions)
{
RUN_TEST_CASE(UnityCommandOptions, DefaultOptions);
RUN_TEST_CASE(UnityCommandOptions, OptionVerbose);
RUN_TEST_CASE(UnityCommandOptions, OptionDryRun);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroup);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByName);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByGroupExact);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectTestByNameExact);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount);
RUN_TEST_CASE(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount);
RUN_TEST_CASE(UnityCommandOptions, MultipleOptions);