diff --git a/docs/docsite/rst/dev_guide/testing_units_modules.rst b/docs/docsite/rst/dev_guide/testing_units_modules.rst index 3ec1a2df247..07d5d51ed16 100644 --- a/docs/docsite/rst/dev_guide/testing_units_modules.rst +++ b/docs/docsite/rst/dev_guide/testing_units_modules.rst @@ -190,8 +190,9 @@ above, either by throwing an exception or ensuring that they haven't been called class AnsibleExitJson(Exception): """Exception class to be raised by module.exit_json and caught by the test case""" pass - #you may also do the same to fail json - module=MagicMock() + + # you may also do the same to fail json + module = MagicMock() module.exit_json.side_effect = AnsibleExitJson(Exception) with self.assertRaises(AnsibleExitJson) as result: return = my_module.test_this_function(module, argument) @@ -300,14 +301,14 @@ variable is set it will be treated as if the input came on ``STDIN`` to the modu args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) basic._ANSIBLE_ARGS = to_bytes(args) - simply call that function before setting up your module +simply call that function before setting up your module:: - def test_already_registered(self): - set_module_args({ - 'activationkey': 'key', - 'username': 'user', - 'password': 'pass', - }) + def test_already_registered(self): + set_module_args({ + 'activationkey': 'key', + 'username': 'user', + 'password': 'pass', + }) Handling exit correctly ----------------------- @@ -334,8 +335,9 @@ testing for the correct exception:: 'username': 'user', 'password': 'pass', }) - with self.assertRaises(AnsibleExitJson) as result: - my_module.main() + + with self.assertRaises(AnsibleExitJson) as result: + my_module.main() The same technique can be used to replace :meth:`module.fail_json` (which is used for failure returns from modules) and for the ``aws_module.fail_json_aws()`` (used in modules for Amazon @@ -349,7 +351,7 @@ the arguments as above, set up the appropriate exit exception and then run the m # This test is based around pytest's features for individual test functions import pytest - import ansible.modules.module.group.my_modulle as my_module + import ansible.modules.module.group.my_module as my_module def test_main_function(monkeypatch): monkeypatch.setattr(my_module.AnsibleModule, "exit_json", fake_exit_json) @@ -511,7 +513,7 @@ This now makes it possible to run tests against the module initiation function:: }) with self.assertRaises(AnsibleFailJson) as result: - self.module.setup_json + self.module.setup_json See also ``test/units/module_utils/aws/test_rds.py`` @@ -559,8 +561,8 @@ the code in Ansible to trigger that failure. General advice on testing Python code `Uncle Bob's many videos on YouTube `_ Unit testing is a part of the of various philosophies of software development, including - Extreme Programming (XP), Clean Coding. Uncle Bob talks through how to benfit from this - `"Why Most Unit Testing is Waste" https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf` + Extreme Programming (XP), Clean Coding. Uncle Bob talks through how to benefit from this + `"Why Most Unit Testing is Waste" `_ An article warning against the costs of unit testing - `'A Response to "Why Most Unit Testing is Waste"' https://henrikwarne.com/2014/09/04/a-response-to-why-most-unit-testing-is-waste/` + `'A Response to "Why Most Unit Testing is Waste"' `_ An response pointing to how to maintain the value of unit tests