1# coding: utf-8 2from unittest import TestCase 3from sure import expect 4from httpretty import httprettified, HTTPretty 5 6try: 7 import urllib.request as urllib2 8except ImportError: 9 import urllib2 10 11 12@httprettified 13def test_decor(): 14 HTTPretty.register_uri( 15 HTTPretty.GET, "http://localhost/", 16 body="glub glub") 17 18 fd = urllib2.urlopen('http://localhost/') 19 got1 = fd.read() 20 fd.close() 21 22 expect(got1).to.equal(b'glub glub') 23 24 25@httprettified 26class DecoratedNonUnitTest(object): 27 28 def test_fail(self): 29 raise AssertionError('Tests in this class should not ' 30 'be executed by the test runner.') 31 32 def test_decorated(self): 33 HTTPretty.register_uri( 34 HTTPretty.GET, "http://localhost/", 35 body="glub glub") 36 37 fd = urllib2.urlopen('http://localhost/') 38 got1 = fd.read() 39 fd.close() 40 41 expect(got1).to.equal(b'glub glub') 42 43 44class NonUnitTestTest(TestCase): 45 """ 46 Checks that the test methods in DecoratedNonUnitTest were decorated. 47 """ 48 49 def test_decorated(self): 50 DecoratedNonUnitTest().test_decorated() 51 52 53@httprettified 54class ClassDecorator(TestCase): 55 56 def test_decorated(self): 57 HTTPretty.register_uri( 58 HTTPretty.GET, "http://localhost/", 59 body="glub glub") 60 61 fd = urllib2.urlopen('http://localhost/') 62 got1 = fd.read() 63 fd.close() 64 65 expect(got1).to.equal(b'glub glub') 66 67 def test_decorated2(self): 68 HTTPretty.register_uri( 69 HTTPretty.GET, "http://localhost/", 70 body="buble buble") 71 72 fd = urllib2.urlopen('http://localhost/') 73 got1 = fd.read() 74 fd.close() 75 76 expect(got1).to.equal(b'buble buble') 77 78 79@httprettified 80class ClassDecoratorWithSetUp(TestCase): 81 82 def setUp(self): 83 HTTPretty.register_uri( 84 HTTPretty.GET, "http://localhost/", 85 responses=[ 86 HTTPretty.Response("glub glub"), 87 HTTPretty.Response("buble buble"), 88 ]) 89 90 def test_decorated(self): 91 92 fd = urllib2.urlopen('http://localhost/') 93 got1 = fd.read() 94 fd.close() 95 96 expect(got1).to.equal(b'glub glub') 97 98 fd = urllib2.urlopen('http://localhost/') 99 got2 = fd.read() 100 fd.close() 101 102 expect(got2).to.equal(b'buble buble') 103 104 def test_decorated2(self): 105 106 fd = urllib2.urlopen('http://localhost/') 107 got1 = fd.read() 108 fd.close() 109 110 expect(got1).to.equal(b'glub glub') 111 112 fd = urllib2.urlopen('http://localhost/') 113 got2 = fd.read() 114 fd.close() 115 116 expect(got2).to.equal(b'buble buble') 117