src/eric7/Testing/Interfaces/UnittestRunner.py

branch
eric7
changeset 10405
df7e1694d0eb
parent 10404
f7d9c31f0c38
child 10425
0badf8f80d7c
equal deleted inserted replaced
10404:f7d9c31f0c38 10405:df7e1694d0eb
244 "tests": self.__testsRun, 244 "tests": self.__testsRun,
245 } 245 }
246 ) 246 )
247 247
248 248
249 def _assembleTestCasesList(suite): 249 def _assembleTestCasesList(suite, start):
250 """ 250 """
251 Protected function to assemble a list of test cases included in a test 251 Protected function to assemble a list of test cases included in a test
252 suite. 252 suite.
253 253
254 @param suite test suite to be inspected 254 @param suite test suite to be inspected
255 @type unittest.TestSuite 255 @type unittest.TestSuite
256 @return list of tuples containing the test case ID, the string 256 @param start name of directory discovery was started at
257 representation and the short description 257 @type str
258 @rtype list of tuples of (str, str, str) 258 @return list of tuples containing the test case ID, the string representation,
259 a short description and the path of the test file name
260 @rtype list of tuples of (str, str, str, str)
259 """ 261 """
260 testCases = [] 262 testCases = []
261 for test in suite: 263 for test in suite:
262 if isinstance(test, unittest.TestSuite): 264 if isinstance(test, unittest.TestSuite):
263 testCases.extend(_assembleTestCasesList(test)) 265 testCases.extend(_assembleTestCasesList(test, start))
264 else: 266 else:
265 testId = test.id() 267 testId = test.id()
266 if ( 268 if (
267 "ModuleImportFailure" not in testId 269 "ModuleImportFailure" not in testId
268 and "LoadTestsFailure" not in testId 270 and "LoadTestsFailure" not in testId
269 and "_FailedTest" not in testId 271 and "_FailedTest" not in testId
270 ): 272 ):
271 testCases.append((testId, str(test), test.shortDescription())) 273 filename = os.path.join(
274 start, test.__module__.replace(".", os.sep) + ".py"
275 )
276 testCases.append((testId, str(test), test.shortDescription(), filename))
272 return testCases 277 return testCases
273 278
274 279
275 def runtest(argv): 280 def runtest(argv, discoverOnly=False):
276 """ 281 """
277 Function to run the tests. 282 Function to run and/or discover the tests.
278 283
279 @param argv list of command line parameters. 284 @param argv list of command line parameters.
280 @type list of str 285 @type list of str
286 @param discoverOnly flag indicating to just discover the available test cases
287 (defaults to False)
288 @type bool (optional)
281 """ 289 """
282 from eric7.EricNetwork.EricJsonStreamWriter import EricJsonWriter 290 from eric7.EricNetwork.EricJsonStreamWriter import EricJsonWriter
283 291
284 writer = EricJsonWriter(argv[0], int(argv[1])) 292 writer = EricJsonWriter(argv[0], int(argv[1]))
285 del argv[:2] 293 del argv[:2]
328 failed = argv[2:] 336 failed = argv[2:]
329 else: 337 else:
330 failed = [] 338 failed = []
331 if discover: 339 if discover:
332 testFileName = testName = "" 340 testFileName = testName = ""
341 testCases = argv[:]
333 else: 342 else:
334 testFileName, testName = argv[:2] 343 testFileName, testName = argv[:2]
335 del argv[:2] 344 del argv[:2]
336
337 testCases = argv[:]
338 345
339 if testFileName: 346 if testFileName:
340 sys.path.insert(1, os.path.dirname(os.path.abspath(testFileName))) 347 sys.path.insert(1, os.path.dirname(os.path.abspath(testFileName)))
341 elif discoveryStart: 348 elif discoveryStart:
342 sys.path.insert(1, os.path.abspath(discoveryStart)) 349 sys.path.insert(1, os.path.abspath(discoveryStart))
409 sys.exit(1) 416 sys.exit(1)
410 417
411 collectedTests = { 418 collectedTests = {
412 "event": "collected", 419 "event": "collected",
413 "tests": [ 420 "tests": [
414 {"id": id, "name": name, "description": desc} 421 {"id": id, "name": name, "description": desc, "filename": filename}
415 for id, name, desc in _assembleTestCasesList(test) 422 for id, name, desc, filename in _assembleTestCasesList(test, discoveryStart)
416 ], 423 ],
417 } 424 }
418 writer.write(collectedTests) 425 writer.write(collectedTests)
419 426
420 testResult = EricTestResult(writer, failfast) 427 if not discoverOnly:
421 startTestRun = getattr(testResult, "startTestRun", None) 428 testResult = EricTestResult(writer, failfast)
422 if startTestRun is not None: 429 startTestRun = getattr(testResult, "startTestRun", None)
423 startTestRun() 430 if startTestRun is not None:
424 try: 431 startTestRun()
425 test.run(testResult) 432 try:
426 finally: 433 test.run(testResult)
427 if cover: 434 finally:
428 cover.stop() 435 if cover:
429 cover.save() 436 cover.stop()
430 writer.write( 437 cover.save()
431 { 438 writer.write(
432 "event": "coverage", 439 {
433 "file": covDataFile, 440 "event": "coverage",
434 } 441 "file": covDataFile,
435 ) 442 }
436 stopTestRun = getattr(testResult, "stopTestRun", None) 443 )
437 if stopTestRun is not None: 444 stopTestRun = getattr(testResult, "stopTestRun", None)
438 stopTestRun() 445 if stopTestRun is not None:
446 stopTestRun()
439 447
440 writer.close() 448 writer.close()
441 sys.exit(0) 449 sys.exit(0)
442 450
443 451
460 468
461 elif command == "runtest": 469 elif command == "runtest":
462 runtest(sys.argv[2:]) 470 runtest(sys.argv[2:])
463 sys.exit(0) 471 sys.exit(0)
464 472
473 elif command == "discovery":
474 runtest(sys.argv[2:], discoverOnly=True)
475 sys.exit(0)
476
465 sys.exit(42) 477 sys.exit(42)
466 478
467 # 479 #
468 # eflag: noqa = M801 480 # eflag: noqa = M801

eric ide

mercurial