Просмотр исходного кода

Added dataProvider decorator

master
David Winterbottom 14 лет назад
Родитель
Сommit
496a145bc5
2 измененных файлов: 38 добавлений и 0 удалений
  1. 17
    0
      oscar/apps/shipping/tests.py
  2. 21
    0
      oscar/test/decorators.py

+ 17
- 0
oscar/apps/shipping/tests.py Просмотреть файл

@@ -7,6 +7,7 @@ from oscar.apps.shipping.methods import FreeShipping, FixedPriceShipping
7 7
 from oscar.apps.shipping.models import OrderAndItemLevelChargeMethod
8 8
 from oscar.apps.basket.models import Basket
9 9
 from oscar.test.helpers import create_product
10
+from oscar.test.decorators import dataProvider
10 11
 
11 12
 class FreeShippingTest(unittest.TestCase):
12 13
     
@@ -17,6 +18,7 @@ class FreeShippingTest(unittest.TestCase):
17 18
         self.assertEquals(D('0.00'), method.basket_charge_incl_tax())
18 19
         self.assertEquals(D('0.00'), method.basket_charge_excl_tax())
19 20
         
21
+        
20 22
 class FixedPriceShippingTest(unittest.TestCase):        
21 23
     
22 24
     def test_fixed_price_shipping_charges_for_empty_basket(self):
@@ -32,6 +34,19 @@ class FixedPriceShippingTest(unittest.TestCase):
32 34
         method.set_basket(basket)
33 35
         self.assertEquals(D('10.00'), method.basket_charge_excl_tax())
34 36
         
37
+    shipping_values = lambda: [('1.00',), 
38
+                               ('5.00',), 
39
+                               ('10.00',), 
40
+                               ('12.00',)]    
41
+        
42
+    @dataProvider(shipping_values)    
43
+    def test_different_values(self, value):
44
+        method = FixedPriceShipping(D(value))
45
+        basket = Basket()
46
+        method.set_basket(basket)
47
+        self.assertEquals(D(value), method.basket_charge_excl_tax())
48
+        
49
+        
35 50
 class OrderAndItemLevelChargeMethodTest(unittest.TestCase):
36 51
     
37 52
     def setUp(self):
@@ -52,6 +67,7 @@ class OrderAndItemLevelChargeMethodTest(unittest.TestCase):
52 67
         self.basket.add_product(p, 7)
53 68
         self.assertEquals(D('5.00') + 7*D('1.00'), self.method.basket_charge_incl_tax())
54 69
 
70
+
55 71
 class ZeroFreeShippingThresholdTest(unittest.TestCase):
56 72
     
57 73
     def setUp(self):
@@ -67,6 +83,7 @@ class ZeroFreeShippingThresholdTest(unittest.TestCase):
67 83
         self.basket.add_product(p)
68 84
         self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
69 85
 
86
+
70 87
 class NonZeroFreeShippingThresholdTest(unittest.TestCase):
71 88
     
72 89
     def setUp(self):

+ 21
- 0
oscar/test/decorators.py Просмотреть файл

@@ -0,0 +1,21 @@
1
+def dataProvider(fn_data_provider):
2
+    """
3
+    Data provider decorator, allows another callable to provide the data for the test.  
4
+    This is a nice feature from PHPUnit which is very useful.
5
+    
6
+    Implementation basd on http://melp.nl/2011/02/phpunit-style-dataprovider-in-python-unit-test/#more-525
7
+    """
8
+    def test_decorator(test_method):
9
+        def execute_test_method_with_each_data_set(self):
10
+            for data in fn_data_provider():
11
+                if len(data) == 2 and isinstance(data[0], tuple) and isinstance(data[1], dict):
12
+                    # Both args and kwargs being provided
13
+                    args, kwargs = data[:]
14
+                else:
15
+                    args, kwargs = data, {}
16
+                try:
17
+                    test_method(self, *args, **kwargs)
18
+                except AssertionError, e:
19
+                    self.fail("%s (Provided data: %s, %s)" % (e, args, kwargs))
20
+        return execute_test_method_with_each_data_set
21
+    return test_decorator

Загрузка…
Отмена
Сохранить