def test_raise_if_not_needed(self): # define decorated function without any socket activity - this either # raises a UserWarning or Exception depending on raise_if_not_needed # option @vcr(debug=True) def temp_test(): pass # run the test - recording mode - raises a UserWarning (default) with catch_stdout() as out: with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") temp_test() self.assertIn('VCR RECORDING', out.getvalue()) self.assertIn('no socket activity', str(w[-1].message)) self.assertEquals(w[-1].category, UserWarning) # .vcr file should not exist self.assertEqual(os.path.exists(self.temp_test_vcr), False) # now enable playback_only mode VCRSystem.raise_if_not_needed = True # re-run the test - recording mode - raises an Exception with catch_stdout() as out: with self.assertRaisesRegex(Exception, 'no socket activity'): temp_test() self.assertIn('VCR RECORDING', out.getvalue()) # .vcr file should not exist self.assertEqual(os.path.exists(self.temp_test_vcr), False)
def test_playback_only(self): # define decorated function without existing vcr tape @vcr(debug=True) def temp_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # .vcr file should not exist self.assertEqual(os.path.exists(self.temp_test_vcr), False) # now enable playback_only mode VCRSystem.playback_only = True # run the test - playback mode but raises exception due to missing tape with catch_stdout() as out: with self.assertRaisesRegex(IOError, "Missing VCR tape file"): temp_test() self.assertIn('VCR PLAYBACK', out.getvalue()) # .vcr file should not exist self.assertEqual(os.path.exists(self.temp_test_vcr), False) # reset VCRSystem.reset() # re-run the test - now record mode without exception with catch_stdout() as out: temp_test() self.assertIn('VCR RECORDING', out.getvalue()) # now .vcr file should exist self.assertEqual(os.path.exists(self.temp_test_vcr), True)
def test_disabled(self): # no disabled but debug mode on decorator level @vcr(debug=True) def read_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # run the test - there should be output due to debug with catch_stdout() as out: read_test() self.assertIn('VCR PLAYBACK', out.getvalue()) # now enable disabled mode VCRSystem.disabled = True # re-run the test - there should be no output with catch_stdout() as out: read_test() self.assertEqual(out.getvalue(), '') # reset VCRSystem.reset() # re-run the test - again output due to debug mode on decorator level with catch_stdout() as out: read_test() self.assertIn('VCR PLAYBACK', out.getvalue())
def test_life_cycle(self): # define function with @vcr decorator and enable debug mode @vcr(debug=True) def temp_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # an initial run of our little test will start in recording mode # and auto-generate a .vcr file - however, this file shouldn't exist at # the moment self.assertEqual(os.path.exists(self.temp_test_vcr), False) # run the test with catch_stdout() as out: temp_test() # debug mode should state its in recording mode self.assertIn('VCR RECORDING', out.getvalue()) # now the .vcr file should exist self.assertEqual(os.path.exists(self.temp_test_vcr), True) # re-run the test - this time it should be using the recorded file with catch_stdout() as out: temp_test() # debug mode should state its in playback mode self.assertIn('VCR PLAYBACK', out.getvalue())
def test_playback_with_debug(self): # define function with decorator @vcr(debug=True) def read_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # run the test with catch_stdout() as out: read_test() self.assertIn('VCR PLAYBACK', out.getvalue())
def test_playback(self): # define function with decorator @vcr def read_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # run the test with catch_stdout() as out: read_test() self.assertEqual(out.getvalue(), '')
def test_record(self): # define function with @vcr decorator @vcr def temp_test(): r = urlopen('https://www.python.org/') self.assertEqual(r.code, 200) # .vcr file should not exist at the moment self.assertEqual(os.path.exists(self.temp_test_vcr), False) # run the test with catch_stdout() as out: temp_test() self.assertEqual(out.getvalue(), '') # .vcr file should now exist self.assertEqual(os.path.exists(self.temp_test_vcr), True)