aboutsummaryrefslogtreecommitdiffstats
path: root/docs/testing/developer/devguide
diff options
context:
space:
mode:
authorEmma Foley <emma.l.foley@intel.com>2018-08-15 09:55:07 +0100
committerEmma Foley <emma.l.foley@intel.com>2018-10-30 14:23:22 +0000
commitb7d1b4c159ff66396d6c035d6cc0df115b3cf5e3 (patch)
treed8108d4fa784fa9829d7429c461a3eb59b02de90 /docs/testing/developer/devguide
parentc1f5f3c5966da1805da36ee1ef739bd658251eb8 (diff)
[docs] Add conventions for writing unit tests.
Added details of conventions used when writing unit tests. JIRA: YARDSTICK-1335 Change-Id: I7bb141126968c88c8a2d7e0dce4f33ed6f79cee4 Signed-off-by: Emma Foley <emma.l.foley@intel.com>
Diffstat (limited to 'docs/testing/developer/devguide')
-rwxr-xr-xdocs/testing/developer/devguide/devguide.rst51
1 files changed, 51 insertions, 0 deletions
diff --git a/docs/testing/developer/devguide/devguide.rst b/docs/testing/developer/devguide/devguide.rst
index 43672cbda..4fe01c12b 100755
--- a/docs/testing/developer/devguide/devguide.rst
+++ b/docs/testing/developer/devguide/devguide.rst
@@ -656,6 +656,57 @@ running ``tox``::
rm -rf .tox/<environment_name>
rm -rf .tox/py27
+Writing unit tests
+~~~~~~~~~~~~~~~~~~
+For each change submitted, a set of unit tests should be submitted, which
+should include both positive and negative testing.
+
+In order to help identify which tests are needed, follow the guidelines below.
+
+* In general, there should be a separate test for each branching point, return
+ value and input set.
+* Negative tests should be written to make sure exceptions are raised and/or
+ handled appropriately.
+
+The following convention should be used for naming tests::
+
+ test_<method_name>_<some_comment>
+
+The comment gives more information on the nature of the test, the side effect
+being checked, or the parameter being modified::
+
+ test_my_method_runtime_error
+ test_my_method_invalid_credentials
+ test_my_method_param1_none
+
+Mocking
++++++++
+The ``mock`` library is used for unit testing to stub out external libraries.
+
+The following conventions are used in Yardstick:
+
+* Use ``mock.patch.object`` instead of ``mock.patch``.
+
+* When naming mocked classes/functions, use ``mock_<class_and_function_name>``
+ e.g. ``mock_subprocess_call``
+
+* Avoid decorating classes with mocks. Apply the mocking in ``setUp()``::
+
+ @mock.patch.object(ssh, 'SSH')
+ class MyClassTestCase(unittest.TestCase):
+
+ should be::
+
+ class MyClassTestCase(unittest.TestCase):
+ def setUp(self):
+ self._mock_ssh = mock.patch.object(ssh, 'SSH')
+ self.mock_ssh = self._mock_ssh.start()
+
+ self.addCleanup(self._stop_mocks)
+
+ def _stop_mocks(self):
+ self._mock_ssh.stop()
+
Plugins
-------