Skip to content

Commit

Permalink
Make it easy to define default or global settings (such as user crede…
Browse files Browse the repository at this point in the history
…ntials) 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
  • Loading branch information
wahuneke committed Apr 12, 2016
1 parent 263a6d0 commit 44538c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
59 changes: 59 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
--------
Expand Down
18 changes: 11 additions & 7 deletions skd_smoke/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 44538c8

Please sign in to comment.