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,6 +190,7 @@ 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()
module.exit_json.side_effect = AnsibleExitJson(Exception)
@ -300,7 +301,7 @@ 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({
@ -334,6 +335,7 @@ testing for the correct exception::
'username': 'user',
'password': 'pass',
})
with self.assertRaises(AnsibleExitJson) as result:
my_module.main()
@ -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)
@ -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 <https://www.youtube.com/watch?v=QedpQjxBPMA&list=PLlu0CT-JnSasQzGrGzddSczJQQU7295D2>`_
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" <https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf>`_
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