5555# pytest 4.2 handles unittest setup/teardown itself via wrapping fixtures.
5656_handle_unittest_methods = parse_version (pytest .__version__ ) < parse_version ("4.2" )
5757
58+ _report_header = []
59+
5860
5961# ############### pytest hooks ################
6062
@@ -287,39 +289,25 @@ def pytest_load_initial_conftests(early_config, parser, args):
287289 ):
288290 os .environ [INVALID_TEMPLATE_VARS_ENV ] = "true"
289291
290- # Configure DJANGO_SETTINGS_MODULE
291- if options .ds :
292- ds_source = "command line option"
293- ds = options .ds
294- elif SETTINGS_MODULE_ENV in os .environ :
295- ds = os .environ [SETTINGS_MODULE_ENV ]
296- ds_source = "environment variable"
297- elif early_config .getini (SETTINGS_MODULE_ENV ):
298- ds = early_config .getini (SETTINGS_MODULE_ENV )
299- ds_source = "ini file"
300- else :
301- ds = None
302- ds_source = None
292+ def _get_option_with_source (option , envname ):
293+ if option :
294+ return option , "option"
295+ if envname in os .environ :
296+ return os .environ [envname ], "env"
297+ cfgval = early_config .getini (envname )
298+ if cfgval :
299+ return cfgval , "ini"
300+ return None , None
303301
304- if ds :
305- early_config ._dsm_report_header = "Django settings: %s (from %s)" % (
306- ds ,
307- ds_source ,
308- )
309- else :
310- early_config ._dsm_report_header = None
311-
312- # Configure DJANGO_CONFIGURATION
313- dc = (
314- options .dc
315- or os .environ .get (CONFIGURATION_ENV )
316- or early_config .getini (CONFIGURATION_ENV )
317- )
302+ ds , ds_source = _get_option_with_source (options .ds , SETTINGS_MODULE_ENV )
303+ dc , dc_source = _get_option_with_source (options .dc , CONFIGURATION_ENV )
318304
319305 if ds :
306+ _report_header .append ("settings: %s (from %s)" % (ds , ds_source ))
320307 os .environ [SETTINGS_MODULE_ENV ] = ds
321308
322309 if dc :
310+ _report_header .append ("configuration: %s (from %s)" % (dc , dc_source ))
323311 os .environ [CONFIGURATION_ENV ] = dc
324312
325313 # Install the django-configurations importer
@@ -338,8 +326,8 @@ def pytest_load_initial_conftests(early_config, parser, args):
338326
339327
340328def pytest_report_header (config ):
341- if config . _dsm_report_header :
342- return [config . _dsm_report_header ]
329+ if _report_header :
330+ return ["django: " + ", " . join ( _report_header ) ]
343331
344332
345333@pytest .mark .trylast
0 commit comments