11"""This module contains code for running a specific test set with mapping database updating"""
22import os
3- import sys
4- from timeit import default_timer as timer
53
64import coverage
7- import pytest
8- from _pytest .python import Function
95
106from pytest_rts .pytest .fake_item import FakeItem
11- from pytest_rts .utils .common import calculate_func_lines
12- from pytest_rts .utils . mappinghelper import TestrunData
7+ from pytest_rts .utils .common import calculate_func_lines , filter_and_sort_pytest_items
8+ from pytest_rts .pytest . mapper_plugin import MapperPlugin
139
1410
1511def _read_testfile_functions (testfile_path ):
@@ -22,20 +18,14 @@ def _read_testfile_functions(testfile_path):
2218 return {}
2319
2420
25- class UpdatePhasePlugin :
21+ class UpdatePhasePlugin ( MapperPlugin ) :
2622 """Class to handle running of selected tests and updating mapping with the results"""
2723
2824 def __init__ (self , test_set , mappinghelper , testgetter ):
2925 """Constructor opens database connection and initializes Coverage.py"""
30- self .cov = coverage .Coverage ()
31- self .cov ._warn_unimported_source = False
26+ super ().__init__ (mappinghelper )
3227 self .test_set = test_set
33-
34- self .mappinghelper = mappinghelper
3528 self .testgetter = testgetter
36-
37- self .testfiles = {testfile [1 ] for testfile in self .mappinghelper .testfiles }
38- self .test_func_lines = None
3929 self .test_func_times = self .testgetter .test_function_runtimes
4030
4131 def pytest_collection_modifyitems (self , session , config , items ):
@@ -45,15 +35,9 @@ def pytest_collection_modifyitems(self, session, config, items):
4535 """
4636 del config
4737 original_length = len (items )
48- selected = list (filter (lambda item : item .nodeid in self .test_set , items ))
49- updated_runtimes = {
50- item .nodeid : self .test_func_times [item .nodeid ]
51- if item .nodeid in self .test_func_times
52- else sys .maxsize
53- for item in selected
54- }
55-
56- items [:] = sorted (selected , key = lambda item : updated_runtimes [item .nodeid ])
38+ items [:] = filter_and_sort_pytest_items (
39+ self .test_set , items , self .test_func_times
40+ )
5741
5842 self .testfiles .update ({os .path .relpath (item .location [0 ]) for item in items })
5943 self .test_func_lines = {
@@ -62,29 +46,5 @@ def pytest_collection_modifyitems(self, session, config, items):
6246 }
6347
6448 session .config .hook .pytest_deselected (
65- items = ([FakeItem (session .config )] * (original_length - len (selected )))
49+ items = ([FakeItem (session .config )] * (original_length - len (items )))
6650 )
67-
68- @pytest .hookimpl (hookwrapper = True )
69- def pytest_runtest_protocol (self , item , nextitem ):
70- """Start coverage collection for each test function run and save data"""
71- del nextitem
72- if isinstance (item , Function ):
73- start = timer ()
74- self .cov .erase ()
75- self .cov .start ()
76- yield
77- self .cov .stop ()
78- end = timer ()
79- elapsed = round (end - start , 4 )
80-
81- testrun_data = TestrunData (
82- pytest_item = item ,
83- elapsed_time = elapsed ,
84- coverage_data = self .cov .get_data (),
85- found_testfiles = self .testfiles ,
86- test_function_lines = self .test_func_lines ,
87- )
88- self .mappinghelper .save_testrun_data (testrun_data )
89- else :
90- yield
0 commit comments