def testassertWarnsTwice(self): """Issue warning twice, test fails""" with self.assertRaises(AssertionError): with spacepy_testing.assertWarns(self, 'always', r'f.*', SPTestWarning): warnings.warn('foo', SPTestWarning) warnings.warn('foo2', SPTestWarning)
def test_deprecation(self): """Deprecation warning on old name""" with spacepy_testing.assertWarns( self, 'always', r'Use spacepy\.plot\.Spectrogram \(capitalized\)\.', DeprecationWarning, r'spacepy.plot$'): a = spacepy.plot.spectrogram( self.data, variables=self.kwargs['variables'])
def testDeprecationNone(self): """Test the deprecation decorator with no docstring""" @spacepy.deprecated(0.1, 'pithy message') def testfunc(x): return x + 1 self.assertEqual( "\n" " .. deprecated:: 0.1\n" " pithy message", testfunc.__doc__) with spacepy_testing.assertWarns(self, 'always', r'pithy message$', DeprecationWarning, r'spacepy$'): self.assertEqual(2, testfunc(1))
def testassertWarnsfilter(self): """assertWarns only acts on desired warnings""" with warnings.catch_warnings(record=True) as cm: with spacepy_testing.assertWarns(self, 'always', r'f.*', SPTestWarning): warnings.warn('foo', SPTestWarning) warnings.warn('foo') warnings.warn('bar', SPTestWarning) # Verify that the other two warnings were issued as expected self.assertEqual(2, len(cm)) for w in cm: self.assertIn(w.category, (UserWarning, SPTestWarning)) if w.category is UserWarning: self.assertEqual('foo', str(w.message)) else: self.assertEqual('bar', str(w.message))
def testDeprecationDifferentMessage(self): """Test the deprecation decorator, docstring different from message""" @spacepy.deprecated(0.1, 'pithy message', docstring='foo\nbar') def testfunc(x): """test function this will test things """ return x + 1 self.assertEqual( "test function\n" "\n" " .. deprecated:: 0.1\n" " foo\n" " bar\n" "\n" " this will test things\n" " ", testfunc.__doc__) with spacepy_testing.assertWarns(self, 'always', r'pithy message$', DeprecationWarning, r'spacepy$'): self.assertEqual(2, testfunc(1))
def testDeprecation(self): """Test the deprecation decorator""" @spacepy.deprecated(0.1, 'pithy message') def testfunc(x): """ test function this will test things """ return x + 1 self.assertEqual( "\n" " test function\n" "\n" " .. deprecated:: 0.1\n" " pithy message\n" "\n" " this will test things\n" " ", testfunc.__doc__) with spacepy_testing.assertWarns(self, 'always', r'pithy message$', DeprecationWarning, r'spacepy$'): self.assertEqual(2, testfunc(1))
def testassertWarnsSimple(self): """Simple test of assertWarns""" # This is most similar to how it would normally be used with spacepy_testing.assertWarns(self, 'always', r'f.*', SPTestWarning): warnings.warn('foo', SPTestWarning)