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