Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.8] Update unit testing docs #55366

Merged
merged 1 commit into from
Apr 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions docs/docsite/rst/dev_guide/testing_units_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
-----------------------
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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``

Expand Down Expand Up @@ -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