FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfocunit.c
1 /*********************************************************************
2  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *********************************************************************/
17 
18 #include "libfocunit.h"
19 
39 int focunit_main(int argc, char **argv, char *test_name, CU_SuiteInfo *suites)
40 {
41  int iopt;
42  char *SuiteName = 0;
43  char *TestName = 0;
44  CU_pTestRegistry pRegistry;
45  CU_pSuite pSuite;
46  CU_pTest pTest;
47  CU_ErrorCode ErrCode;
48  CU_pFailureRecord FailureList;
49  CU_RunSummary *pRunSummary;
50  int FailRec;
51 
53  if (!test_name)
54  {
55  fprintf(stderr, "FATAL: empty test name.\n");
56  exit(1);
57  }
58 
59  if (CU_initialize_registry())
60  {
61  fprintf(stderr, "FATAL: Initialization of Test Registry failed.\n");
62  exit(1);
63  }
64 
65  assert(CU_get_registry());
66  assert(!CU_is_test_running());
67 
68  if (CU_register_suites(suites) != CUE_SUCCESS)
69  {
70  fprintf(stderr, "FATAL: Register suites failed - %s\n", CU_get_error_msg());
71  exit(1);
72  }
73  pRegistry = CU_get_registry();
74 
75  /* option -s suitename to run
76  * -t testname to run
77  */
78  while ((iopt = getopt(argc, argv, "s:t:")) != -1)
79  {
80  switch (iopt)
81  {
82  case 's':
83  SuiteName = optarg;
84  break;
85  case 't':
86  TestName = optarg;
87  break;
88  default:
89  fprintf(stderr, "Invalid argument for %s\n", argv[0]);
90  exit(-1);
91  }
92  }
93 
94  /* If TestName is specified, SuiteName is required */
95  if (TestName && !SuiteName)
96  {
97  fprintf(stderr, "A Suite name (-s) is required if you specify a test name.\n");
98  exit(-1);
99  }
100 
101  if (SuiteName)
102  {
103  pSuite = CU_get_suite_by_name(SuiteName, pRegistry);
104  if (!pSuite)
105  {
106  fprintf(stderr, "Suite %s not found.\n", SuiteName);
107  exit(-1);
108  }
109 
110  if (TestName)
111  {
112  pTest = CU_get_test_by_name(TestName, pSuite);
113  if (!pTest)
114  {
115  fprintf(stderr, "Test %s not found in suite %s.\n", TestName, SuiteName);
116  exit(-1);
117  }
118  ErrCode = CU_run_test(pSuite, pTest);
119  }
120  else
121  {
122  ErrCode = CU_run_suite(pSuite);
123  }
124 
125  if (ErrCode)
126  {
127  fprintf(stderr, "Error: %s\n", CU_get_error_msg());
128  exit(-2);
129  }
130  }
131  else // generate xml test report via Automated mode only when run all suits in pRegistrAy, or not
132 
133  {
134  CU_set_output_filename(test_name);
135  CU_list_tests_to_file();
136  CU_automated_run_tests();
137  }
138 
139  pRunSummary = CU_get_run_summary();
140  printf("%s summary:\n", test_name);
141  printf(" Number of suites run: %d\n", pRunSummary->nSuitesRun);
142  printf(" Number of suites failed: %d\n", pRunSummary->nSuitesFailed);
143  printf(" Number of tests run: %d\n", pRunSummary->nTestsRun);
144  printf(" Number of tests failed: %d\n", pRunSummary->nTestsFailed);
145  printf(" Number of asserts: %d\n", pRunSummary->nAsserts);
146  printf(" Number of asserts failed: %d\n", pRunSummary->nAssertsFailed);
147  printf(" Number of failures: %d\n", pRunSummary->nFailureRecords);
148 
149  /* Print any failures */
150  int result = 0;
151  if (pRunSummary->nFailureRecords)
152  {
153  printf("\nFailures:\n");
154  FailRec = 1;
155  for (FailureList = CU_get_failure_list(); FailureList; FailureList = FailureList->pNext)
156  {
157  printf("%d. File: %s Line: %u",
158  FailRec,
159  FailureList->strFileName,
160  FailureList->uiLineNumber);
161 
162  if (FailureList->pTest) {
163  printf(" Test: %s", (FailureList->pTest)->pName);
164  }
165  if (FailureList->pSuite) {
166  printf(" Suite: %s", (FailureList->pSuite)->pName);
167  }
168 
169  printf("\n %s\n",
170  FailureList->strCondition);
171 
172  FailRec++;
173  }
174  printf("\n");
175  result = 1;
176  }
177 
178  CU_cleanup_registry();
179 
180  return result;
181 }
CU_SuiteInfo suites[]
all test suites for delagent
Definition: testRun.h:54