Update unit testing docs (#55272)

* Updated links
* Updated indentation
* Fixed typos

Fixes: #55242

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-04-16 00:57:06 +05:30 committed by Alicia Cozine
parent a8955b0c7c
commit 3003954415

View file

@ -190,8 +190,9 @@ above, either by throwing an exception or ensuring that they haven't been called
class AnsibleExitJson(Exception): class AnsibleExitJson(Exception):
"""Exception class to be raised by module.exit_json and caught by the test case""" """Exception class to be raised by module.exit_json and caught by the test case"""
pass 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) module.exit_json.side_effect = AnsibleExitJson(Exception)
with self.assertRaises(AnsibleExitJson) as result: with self.assertRaises(AnsibleExitJson) as result:
return = my_module.test_this_function(module, argument) 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}) args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(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): def test_already_registered(self):
set_module_args({ set_module_args({
'activationkey': 'key', 'activationkey': 'key',
'username': 'user', 'username': 'user',
'password': 'pass', 'password': 'pass',
}) })
Handling exit correctly Handling exit correctly
----------------------- -----------------------
@ -334,8 +335,9 @@ testing for the correct exception::
'username': 'user', 'username': 'user',
'password': 'pass', '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 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 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 # This test is based around pytest's features for individual test functions
import pytest 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): def test_main_function(monkeypatch):
monkeypatch.setattr(my_module.AnsibleModule, "exit_json", fake_exit_json) 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: with self.assertRaises(AnsibleFailJson) as result:
self.module.setup_json self.module.setup_json
See also ``test/units/module_utils/aws/test_rds.py`` 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 General advice on testing Python code
`Uncle Bob's many videos on YouTube <https://www.youtube.com/watch?v=QedpQjxBPMA&list=PLlu0CT-JnSasQzGrGzddSczJQQU7295D2>`_ `Uncle Bob's many videos on YouTube <https://www.youtube.com/watch?v=QedpQjxBPMA&list=PLlu0CT-JnSasQzGrGzddSczJQQU7295D2>`_
Unit testing is a part of the of various philosophies of software development, including 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 Extreme Programming (XP), Clean Coding. Uncle Bob talks through how to benefit from this
`"Why Most Unit Testing is Waste" https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf` `"Why Most Unit Testing is Waste" <https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf>`_
An article warning against the costs of unit testing 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"' <https://henrikwarne.com/2014/09/04/a-response-to-why-most-unit-testing-is-waste/>`_
An response pointing to how to maintain the value of unit tests An response pointing to how to maintain the value of unit tests