Use Python decorators to define your testing scope

We’ve already had an introduction about the Python decorators in a previous article ‘ Decorate with decorators’. Our next goal is to use their benefits in automation software testing. An important usage which comes to our mind is about grouping different automated tests based on their popularity.

With popularity, I mean how often a test must be triggered or what’s its test scope. It could be part of the BAT, Regression, Smoke, etc. Let’s say, for a daily execution (e.g. nightly job) we will need some tests to be decorated as belonging to BAT. For some less important tests they could be part of the regression scope (for a weekly run), but still, a BAT test could also act for the regression group or we could have other mixes. There are several articles on the internet about the test scope, but now let’s focus on decorators in order to achieve our goal: mark all the tests as part of different scopes.

Step 1 – Write the test scope decorator

def test_scope(*scope):
    def test_decorator(test_function):
        def test_wrapper(*args):
            print(test_function.__name__)
            print(scope)
            # Todo: create a function to call test type
            return scope
        return test_wrapper
    return test_decorator

Here is our function used to define the test scope decorator. First, it should be built to support parameters (in order to send multiple test scopes). Using them and also the test_function.__name__ which is send out as a parameter, the test_wrapperclosure will get access to the test properties. In this example, we are just printing the outcome, but you could practice more on this by e.g. add a statement based on the scope, create a list with the tests to be executed and why not, also trigger them.

Step 2 – Decorate some tests

We will define two tests to see how test scope definition could be achieved based on test scope decorator

@test_scope('BAT', 'Smoke', 'Performance')
def test_search(test):
    print('make a simple search', test)
@test_scope('Regression')
def test_routing(test):
    print('calculate a route', test)

Step 3 – Execution

Now we could run the previously listed tests:

test_search('Find Cluj')
test_routing('my_test')

Output:

test_search
('BAT', 'Smoke', 'Performance')
test_routing
('Regression')

As we can see we’ve printed out the test function name and their scopes. Now you don’t have to create a separate python module, or a JSON or whatever file you want to hardcode all the tests which belong to different scopes.

Conclusion

Using decorators you could facilitate the implementation of different mandatory features of an automation framework. As of grouping your tests based on their scope is a crucial aspect in SW Testing, once you’ve decided to automated your tests, this approach is definitely the most Pythonic way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.