From 44538c8f317143cd08358d33e8cffdc5638544e7 Mon Sep 17 00:00:00 2001 From: Bill Huneke Date: Tue, 12 Apr 2016 16:40:02 -0400 Subject: [PATCH] Make it easy to define default or global settings (such as user credentials) by attaching default values to the class instead of having to define the same values over and over in each test URL definition Update documentation to explain the new 'defaults' feature --- README.rst | 59 +++++++++++++++++++++++++++++++++++++++++++ skd_smoke/__init__.py | 18 ++++++++----- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index a7d0374..c2302b2 100644 --- a/README.rst +++ b/README.rst @@ -40,6 +40,13 @@ django project. Configuration ------------- + +Sub-class **SmokeTestCase** and configure your test using the *TESTS_CONFIGURATION*. Further configure by defining +optional *default_* attributes (or class methods). + +Basic Configuration +******** + ``TESTS_CONFIGURATION`` of your ``TestCase`` should contain tuple/list of tuples for every request with the next structure: @@ -97,6 +104,58 @@ you can use it to transfer state between them. But take into account that #. ``user_credentials`` #. ``request_data`` +Extra Configuration +******** + +For convenience, default values can be provided for some url configuration items. If a URL test is defined which is +missing a definition for one of these values, the class definition is searched for a default value and that is used, if +present. + +For example, you may define *user_credentials* in a URL test. In cases where several URL tests have the same user +credentials, it is more convenient to define the credentials one time, as a class attribute (or method). So in the case +of user credentials, you can also define a class attribute like this, which will take effect for all test urls which +dont have user_credentials defined. Like this: + +.. code-block:: python + + class TestDemo(SmokeTestCase): + TESTS_CONFIGURATION = ( + ('page1', 200, 'GET',), + ('page2', 200, 'GET',), + ('page3', 200, 'GET',), + ('page4', 200, 'GET', { + 'user_credentials': { + 'username': 'other_user', + 'password': 'other_password', + } + }), + ('page5', 200, 'GET', { + 'user_credentials': None, + }), + ) + + default_user_credentials = { + 'username': 'my_user', + 'password': 'my_password', + } + + +In the example above, tests for page1, page2, and page3 will be run using the default credentials (*my_user*). The test +for page4 will run with special credentials (*other_user*). The test for page5 will run with no credentials, no logged +in user. + +**Note:** these default values can be either class attributes *or* class methods (taking a ``self`` parameter). + +The full list of possible, default, attributes is: + +* default_comment +* default_initialize +* default_url_args +* default_url_kwargs +* default_request_data +* default_user_credentials +* default_redirect_to + Examples -------- diff --git a/skd_smoke/__init__.py b/skd_smoke/__init__.py index 8f53e4e..b316cc1 100644 --- a/skd_smoke/__init__.py +++ b/skd_smoke/__init__.py @@ -331,13 +331,17 @@ def __new__(mcs, name, bases, attrs): setattr(cls, fail_method_name, fail_method) else: for urlname, status, method, data in config: - comment = data.get('comment', None) - initialize = data.get('initialize', None) - url_args = data.get('url_args', None) - url_kwargs = data.get('url_kwargs', None) - request_data = data.get('request_data', None) - get_user_credentials = data.get('user_credentials', None) - redirect_to = data.get('redirect_to', None) + # For each config item, if it doesnt exist in config tuple's "data" then + # see if user has defined a default on the test class + find_conf = lambda name: data.get(name, getattr(cls, "default_" + name, None)) + + comment = find_conf('comment') + initialize = find_conf('initialize') + url_args = find_conf('url_args') + url_kwargs = find_conf('url_kwargs') + request_data = find_conf('request_data') + get_user_credentials = find_conf('user_credentials') + redirect_to = find_conf('redirect_to') status_text = STATUS_CODE_TEXT.get(status, 'UNKNOWN') test_method_name = prepare_test_name(urlname, method, status)