Skip to content

Commit d77767b

Browse files
author
Sylvain MARIE
committed
Merge branch 'user/jgibson/delay_numpy_imports' of https://github.com/jmrgibson/python-pytest-harvest into jmrgibson-user/jgibson/delay_numpy_imports
2 parents 4c9f2e6 + cd2b1b5 commit d77767b

1 file changed

Lines changed: 99 additions & 105 deletions

File tree

pytest_harvest/plugin.py

Lines changed: 99 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -219,61 +219,58 @@ def module_results_dct(request, fixture_store):
219219
return get_module_results_dct(request, module_name=request.module.__name__, fixture_store=fixture_store)
220220

221221

222-
try:
223-
import pandas as pd
224-
225-
def get_session_results_df(session_or_request,
226-
fixture_store=FIXTURE_STORE, # type: Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
227-
results_bag_fixture_name='results_bag' # type: str
228-
):
229-
# type: (...) -> pd.DataFrame
230-
"""
231-
Helper method to get exactly the same object than the `session_results_df` fixture, from a session object.
222+
def get_session_results_df(session_or_request,
223+
fixture_store=FIXTURE_STORE, # type: Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
224+
results_bag_fixture_name='results_bag' # type: str
225+
):
226+
# type: (...) -> pd.DataFrame
227+
"""
228+
Helper method to get exactly the same object than the `session_results_df` fixture, from a session object.
232229
This can be useful to retrieve the results from the `pytest_sessionfinish` hook for example.
233230
234231
See `session_results_df` fixture for details.
235232
236-
:param session_or_request: the pytest session or request
237-
:param fixture_store: an optional fixture store
238-
:param results_bag_fixture_name: an optional name for results bag fixture in the fixture store. Default is
239-
"results_bag"
240-
:return:
241-
"""
242-
# in case of xdist, make sure persisted workers results have been reloaded
243-
possibly_restore_xdist_workers_structs(session_or_request)
244-
245-
# get the synthesis dictionary, merged with default fixture store and flattening default results_bag
246-
session_results_dct = get_session_synthesis_dct(session_or_request, durations_in_ms=True,
247-
test_id_format='full', status_details=False,
248-
fixture_store=fixture_store,
249-
flatten=True, flatten_more=results_bag_fixture_name)
250-
251-
# convert to a pandas dataframe
252-
results_df = pd.DataFrame.from_dict(session_results_dct, orient='index')
253-
results_df = results_df.loc[list(session_results_dct.keys()), :] # fix rows order
254-
results_df.index.name = 'test_id' # set index name
255-
256-
# We do not want to post-process according to steps here, this fixture should have as a contract that the keys
257-
# are the True test ids.
258-
#
259-
# try:
260-
# # if pytest_steps is installed, separate the test ids from the step ids
261-
# from pytest_steps import handle_steps_in_results_df
262-
# results_df = handle_steps_in_results_df(results_df, keep_orig_id=True, no_steps_policy='skip')
263-
# except ImportError:
264-
# # pytest_steps is not installed, ok.
265-
# pass
266-
# except Exception as e:
267-
# # other issue: warn about it but continue
268-
# warn("%s: %s" % (e, type(e)))
269-
270-
return results_df
271-
272-
except ImportError as e:
273-
saved_e = e
274-
def get_session_results_df(*args, **kwargs):
233+
:param session_or_request: the pytest session or request
234+
:param fixture_store: an optional fixture store
235+
:param results_bag_fixture_name: an optional name for results bag fixture in the fixture store. Default is
236+
"results_bag"
237+
:return:
238+
"""
239+
try:
240+
import pandas as pd # pylint: disable=import-outside-toplevel
241+
except ImportError as e:
275242
six.raise_from(Exception("There was an error importing `pandas` module. Fixture `session_results_df` and method"
276-
"`get_session_results_df` can not be used in this session."), saved_e)
243+
"`get_session_results_df` can not be used in this session."), e)
244+
245+
# in case of xdist, make sure persisted workers results have been reloaded
246+
possibly_restore_xdist_workers_structs(session_or_request)
247+
248+
# get the synthesis dictionary, merged with default fixture store and flattening default results_bag
249+
session_results_dct = get_session_synthesis_dct(session_or_request, durations_in_ms=True,
250+
test_id_format='full', status_details=False,
251+
fixture_store=fixture_store,
252+
flatten=True, flatten_more=results_bag_fixture_name)
253+
254+
# convert to a pandas dataframe
255+
results_df = pd.DataFrame.from_dict(session_results_dct, orient='index')
256+
results_df = results_df.loc[list(session_results_dct.keys()), :] # fix rows order
257+
results_df.index.name = 'test_id' # set index name
258+
259+
# We do not want to post-process according to steps here, this fixture should have as a contract that the keys
260+
# are the True test ids.
261+
#
262+
# try:
263+
# # if pytest_steps is installed, separate the test ids from the step ids
264+
# from pytest_steps import handle_steps_in_results_df
265+
# results_df = handle_steps_in_results_df(results_df, keep_orig_id=True, no_steps_policy='skip')
266+
# except ImportError:
267+
# # pytest_steps is not installed, ok.
268+
# pass
269+
# except Exception as e:
270+
# # other issue: warn about it but continue
271+
# warn("%s: %s" % (e, type(e)))
272+
273+
return results_df
277274

278275

279276
@pytest.fixture(scope='function')
@@ -292,63 +289,60 @@ def session_results_df(request, fixture_store):
292289
return get_session_results_df(request, fixture_store=fixture_store)
293290

294291

295-
try:
296-
import pandas as pd
297-
298-
def get_filtered_results_df(session,
299-
filter=None, # type: Any
300-
test_id_format='full', # type: str
301-
fixture_store=FIXTURE_STORE, # type: Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
302-
results_bag_fixture_name='results_bag' # type: str
303-
):
304-
# type: (...) -> pd.DataFrame
305-
"""
306-
Combines `get_session_synthesis_dct` with a transformation into a pandas DataFrame.
307-
308-
:param session: the pytest session object
309-
:param filter: any filter, see `get_session_synthesis_dct` for details
310-
:param fixture_store: an optional fixture store
311-
:param results_bag_fixture_name: an optional name for results bag fixture in the fixture store. Default is
312-
"results_bag"
313-
:return:
314-
"""
315-
# in case of xdist, make sure persisted workers results have been reloaded
316-
possibly_restore_xdist_workers_structs(session)
317-
318-
# get the synthesis dictionary, merged with default fixture store and flattening default results_bag
319-
module_results_dct = get_session_synthesis_dct(session, durations_in_ms=True,
320-
filter=filter,
321-
test_id_format=test_id_format, status_details=False,
322-
fixture_store=fixture_store,
323-
flatten=True, flatten_more=results_bag_fixture_name)
324-
325-
# convert to a pandas dataframe
326-
results_df = pd.DataFrame.from_dict(module_results_dct, orient='index')
327-
results_df = results_df.loc[list(module_results_dct.keys()), :] # fix rows order
328-
results_df.index.name = 'test_id' # set index name
329-
330-
# We do not want to post-process according to steps here, this fixture should have as a contract that the keys
331-
# are the True test ids.
332-
#
333-
# try:
334-
# # if pytest_steps is installed, separate the test ids from the step ids
335-
# from pytest_steps import handle_steps_in_results_df
336-
# results_df = handle_steps_in_results_df(results_df, keep_orig_id=True, no_steps_policy='skip')
337-
# except ImportError:
338-
# # pytest_steps is not installed, ok.
339-
# pass
340-
# except Exception as e:
341-
# # other issue: warn about it but continue
342-
# warn("%s: %s" % (e, type(e)))
343-
344-
return results_df
345-
346-
except ImportError as e:
347-
saved_e = e
348-
def get_filtered_results_df(*args, **kwargs):
292+
def get_filtered_results_df(session,
293+
filter=None, # type: Any
294+
test_id_format='full', # type: str
295+
fixture_store=FIXTURE_STORE, # type: Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
296+
results_bag_fixture_name='results_bag' # type: str
297+
):
298+
# type: (...) -> pd.DataFrame
299+
"""
300+
Combines `get_session_synthesis_dct` with a transformation into a pandas DataFrame.
301+
302+
:param session: the pytest session object
303+
:param filter: any filter, see `get_session_synthesis_dct` for details
304+
:param fixture_store: an optional fixture store
305+
:param results_bag_fixture_name: an optional name for results bag fixture in the fixture store. Default is
306+
"results_bag"
307+
:return:
308+
"""
309+
try:
310+
import pandas as pd # pylint: disable=import-outside-toplevel
311+
except ImportError as e:
349312
six.raise_from(Exception("There was an error importing `pandas` module. Fixture `session_results_df` and "
350313
"methods `get_filtered_results_df` and `get_module_results_df` can not be used in this"
351-
" session. "), saved_e)
314+
" session. "), e)
315+
316+
# in case of xdist, make sure persisted workers results have been reloaded
317+
possibly_restore_xdist_workers_structs(session)
318+
319+
# get the synthesis dictionary, merged with default fixture store and flattening default results_bag
320+
module_results_dct = get_session_synthesis_dct(session, durations_in_ms=True,
321+
filter=filter,
322+
test_id_format=test_id_format, status_details=False,
323+
fixture_store=fixture_store,
324+
flatten=True, flatten_more=results_bag_fixture_name)
325+
326+
# convert to a pandas dataframe
327+
results_df = pd.DataFrame.from_dict(module_results_dct, orient='index')
328+
results_df = results_df.loc[list(module_results_dct.keys()), :] # fix rows order
329+
results_df.index.name = 'test_id' # set index name
330+
331+
# We do not want to post-process according to steps here, this fixture should have as a contract that the keys
332+
# are the True test ids.
333+
#
334+
# try:
335+
# # if pytest_steps is installed, separate the test ids from the step ids
336+
# from pytest_steps import handle_steps_in_results_df
337+
# results_df = handle_steps_in_results_df(results_df, keep_orig_id=True, no_steps_policy='skip')
338+
# except ImportError:
339+
# # pytest_steps is not installed, ok.
340+
# pass
341+
# except Exception as e:
342+
# # other issue: warn about it but continue
343+
# warn("%s: %s" % (e, type(e)))
344+
345+
return results_df
352346

353347

354348
def get_module_results_df(session,

0 commit comments

Comments
 (0)