@@ -35,5 +35,70 @@ Docs are generated with [Sphinx](https://docs.readthedocs.io/en/latest/intro/get
3535` make html ` in the softlayer-python/docs directory, which should generate the HTML in softlayer-python/docs/_ build/html for testing.
3636
3737
38+ ## Unit Tests
3839
40+ All new features should be 100% code covered, and your pull request should at the very least increase total code overage.
3941
42+ ### Mocks
43+ To tests results from the API, we keep mock results in SoftLayer/fixtures/<SoftLayer_Service>/ with the method name matching the variable name.
44+
45+ Any call to a service that doesn't have a fixture will result in a TransportError
46+
47+ ### Overriding Fixtures
48+
49+ Adding your expected output in the fixtures file with a unique name is a good way to define a fixture that gets used frequently in a test.
50+
51+ ``` python
52+ from SoftLayer.fixtures import SoftLayer_Product_Package
53+
54+ def test_test (self ):
55+ amock = self .set_mock(' SoftLayer_Product_Package' , ' getAllObjects' )
56+ amock.return_value = fixtures.SoftLayer_Product_Package.RESERVED_CAPACITY
57+ ```
58+
59+ Otherwise defining it on the spot works too.
60+ ``` python
61+ def test_test (self ):
62+ mock = self .set_mock(' SoftLayer_Network_Storage' , ' getObject' )
63+ mock.return_value = {
64+ ' billingItem' : {' hourlyFlag' : True , ' id' : 449 },
65+ }
66+ ```
67+
68+
69+ ### Call testing
70+ Testing your code to make sure it makes the correct API call is also very important.
71+
72+ The testing.TestCase class has a method call ` assert_called_with ` which is pretty handy here.
73+
74+ ``` python
75+ self .assert_called_with(
76+ ' SoftLayer_Billing_Item' , # Service
77+ ' cancelItem' , # Method
78+ args = (True , True , ' ' ), # Args
79+ identifier = 449 , # Id
80+ mask = mock.ANY , # object Mask,
81+ filter = mock.ANY , # object Filter
82+ limit = 0 , # result Limit
83+ offset = 0 # result Offset
84+ )
85+ ```
86+
87+ Making sure a API was NOT called
88+
89+ ``` python
90+ self .assertEqual([], self .calls(' SoftLayer_Account' , ' getObject' ))
91+ ```
92+
93+ Making sure an API call has a specific arg, but you don't want to list out the entire API call (like with a place order test)
94+
95+ ``` python
96+ # Get the API Call signature
97+ order_call = self .calls(' SoftLayer_Product_Order' , ' placeOrder' )
98+
99+ # Get the args property of that API call, which is a tuple, with the first entry being our data.
100+ order_args = getattr (order_call[0 ], ' args' )[0 ]
101+
102+ # Test our specific argument value
103+ self .assertEqual(123 , order_args[' hostId' ])
104+ ```
0 commit comments