avocado: Simplify setting test default values

Instead of using self.params.set_default() manually for
each default value, allow test writers to set a
default_params attribute with default params, and
set them at test class init time. This way we keep
things even shorter, while allowing avocado tests
to be used stand alone.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 c887b5fc
......@@ -39,6 +39,7 @@ class Test(unittest.TestCase):
You'll inherit from this to write your own tests. Tipically you'll want
to implement setup(), action() and cleanup() methods on your own tests.
"""
default_params = {}
def __init__(self, methodName='runTest', name=None, params=None,
base_logdir=None, tag=None, job=None):
......@@ -97,13 +98,26 @@ class Test(unittest.TestCase):
self.log.info('START %s', self.tagged_name)
self.log.debug('')
self.log.debug('Test parameters:')
self.log.debug('Test instance parameters:')
# Set the helper set_default to the params object
setattr(self.params, 'set_default', self._set_default)
# Apply what comes from the params dict
for key in sorted(self.params.keys()):
self.log.debug(' %s = %s', key, self.params.get(key))
setattr(self.params, key, self.params.get(key))
self.log.debug('')
# Apply what comes from the default_params dict
self.log.debug('Default parameters:')
for key in sorted(self.default_params.keys()):
self.log.debug(' %s = %s', key, self.default_params.get(key))
self.params.set_default(key, self.default_params[key])
self.log.debug('')
self.log.debug('Test instance params override defaults whenever available')
self.log.debug('')
self.debugdir = None
self.resultsdir = None
self.status = None
......
......@@ -26,12 +26,7 @@ class sleeptest(test.Test):
"""
Example test for avocado.
"""
def setup(self):
"""
If no config was provided, give self.params.sleep_length a default.
"""
self.params.set_default('sleep_length', 1.0)
default_params = {'sleep_length': 1.0}
def action(self):
"""
......
......@@ -14,7 +14,6 @@
# Copyright: Red Hat Inc. 2013-2014
# Author: Lucas Meneghel Rodrigues <lmr@redhat.com>
import os
from avocado import test
......@@ -29,16 +28,14 @@ class synctest(test.Test):
"""
Execute the synctest test suite.
"""
default_params = {'sync_tarball': 'synctest.tar.bz2',
'sync_length': 100,
'sync_loop': 10}
def setup(self):
"""
Set default params and build the synctest suite.
Build the synctest suite.
"""
# Set all params with default values
self.params.set_default('sync_tarball', 'synctest.tar.bz2')
self.params.set_default('sync_length', 100)
self.params.set_default('sync_loop', 10)
# Build the synctest suite
self.cwd = os.getcwd()
tarball_path = self.get_deps_path(self.params.sync_tarball)
archive.extract(tarball_path, self.srcdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册