@@ -226,31 +226,52 @@ def get_xml_report(self, name=None):
226226 return xml_coverage
227227
228228 @contextlib .contextmanager
229- def assert_plugin_disabled (self , msg ):
230- """Assert that our plugin was disabled during an operation."""
231- # self.run_django_coverage will raise PluginDisabled if the plugin
232- # was disabled.
229+ def assert_coverage_warnings (self , * msgs , min_cov = None ):
230+ """Assert that coverage.py warnings are raised that contain all msgs.
231+
232+ If coverage version isn't at least min_cov, then no warnings are expected.
233+
234+ """
233235 # Coverage.py 6.0 made the warnings real warnings, so we have to adapt
234236 # how we test the warnings based on the version.
235- if coverage .version .version_info >= (6 , 0 ):
237+ if min_cov is not None and coverage .version_info < min_cov :
238+ # Don't check for warnings on lower versions of coverage
239+ yield
240+ return
241+ elif coverage .version_info >= (6 , 0 ):
236242 import coverage .exceptions as cov_exc
237243 ctxmgr = self .assertWarns (cov_exc .CoverageWarning )
238244 else :
239- ctxmgr = nullcontext ()
245+ ctxmgr = contextlib . nullcontext ()
240246 with ctxmgr as cw :
241- with self .assertRaises (PluginDisabled ):
242- yield
247+ yield
243248
244249 if cw is not None :
245250 warn_text = "\n " .join (str (w .message ) for w in cw .warnings )
246251 else :
247252 warn_text = self .stderr ()
248- self .assertIn (
249- "Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' "
250- "due to an exception:" ,
251- warn_text
252- )
253- self .assertIn ("DjangoTemplatePluginException: " + msg , warn_text )
253+ for msg in msgs :
254+ self .assertIn (msg , warn_text )
255+
256+ @contextlib .contextmanager
257+ def assert_plugin_disabled (self , msg ):
258+ """Assert that our plugin was disabled during an operation."""
259+ # self.run_django_coverage will raise PluginDisabled if the plugin
260+ # was disabled.
261+ msgs = [
262+ "Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' due to an exception:" ,
263+ "DjangoTemplatePluginException: " + msg ,
264+ ]
265+ with self .assert_coverage_warnings (* msgs ):
266+ with self .assertRaises (PluginDisabled ):
267+ yield
268+
269+ @contextlib .contextmanager
270+ def assert_no_data (self , min_cov = None ):
271+ """Assert that coverage warns no data was collected."""
272+ warn_msg = "No data was collected. (no-data-collected)"
273+ with self .assert_coverage_warnings (warn_msg , min_cov = min_cov ):
274+ yield
254275
255276
256277def squashed (s ):
@@ -289,9 +310,3 @@ def test_thing(self):
289310class PluginDisabled (Exception ):
290311 """Raised if we find that our plugin has been disabled."""
291312 pass
292-
293-
294- @contextlib .contextmanager
295- def nullcontext ():
296- """For when we need a context manager to do nothing."""
297- yield None
0 commit comments