HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Robot Framework kickstarter - script to run regression test suites

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptrobotsuitesregressiontestkickstarterframeworkrun

Problem

I currently work as a Junior QA tester for a small software business. Before I joined, they had no automation in place to help assist them. I then got hired and resolved this by implementing Robot Framework to their system and juggle between manual testing and creating Robot scripts to assist in regression.

Instead of manually firing off the commands to start the Robot Framework to complete the scripts I made - I decided to create a Python script to do it for me. It started of being a small little time saver. And now it has become something which combines the reports into one giant report and allows to pick and choose the tests you want to fire.

Never touching Python until recently and taking a huge interest into it - I want to make sure I am doing it right and would like any pointers from people with more experience. I have tried my best to follow the PEP8 standard and tried to comment (might be too much...) my code so that others in the office (Who have no Python experience) can understand it too.

Below is my code!

```
"""
File Name : RunAutomation.py
Created : 16/12/16
Edited : 06/03/17
Author : John
Version : 1.3.0
Py Ver : 2.7.11

Change Log
1.3.0 : Implemented the ability to "Pick and choose" the Test Suites you
want. This allows the user to run any specific tests they need to
run and not have to restart the script every time. Original idea
on how this was meant to work. Removed whitespace in InputtedSprint
and InputtedBuild. If all suites is selected, it is not possible to
run other suites also. [A] takes priority.
1.2.8 : Added all the def for all the functions when selecting from the
menu.
1.2.5 : Fixed the extra '/' issue with the
name of the combined report with the Sprint var and fixed the
extra space being added to the list of outputs.
1.2.0 : Added the ability to combine all xml outputs at the end of
choosing a test suite. Com

Solution

Python has a style guide, and you have a few non-compliances:

  • Imports should be alphabetically ordered (and you could combine from collections import Counter, OrderedDict)



  • Functions and variables should be named in snake_case, not CamelCase



  • You should be consistent with ' and " for string quoting



Rather than concatenating strings with +, it's more readable and efficient to use the various string formatting options in Python, e.g. 'Hello {0}'.format('world') rather than 'Hello' + 'world'.

Your code has an awful lot of duplication. Your functions vary only in the specific names being run, the structure is the same. Therefore you could reduce this duplication by extracting the names:

def run_suite(name):
    print "Running {0} Suite . . .\n".format(name)
    print "=" * 78 + "\n"

    os.chdir("C:/TestCases/Robot/Current/EPRAIS/{0}".format(name))
    print "Moving to dir: %s \n" % os.getcwd()

    ...


You also have duplication of information in e.g. which input number relates to which test. One way to simplify might be to use OOP; make a class to abstract away the details of tests:

class TestSuite(object):

    def __init__(self, name, ...):
        self.name = name
        ... 

    def run(self):
        """Call this method to run the suite."""
        ...

    def _change_working_dir(self):
        """Private methods can hide the implementation detail."""
        os.chdir("C:/TestCases/Robot/Current/EPRAIS/{0.name}".format(self))

    ...


Then you can make a list of the numbered tests:

suites = [TestSuite('Login', ...), ...]


and build the help text from it automatically:

for index, suite in enumerate(suites, start=1):
    print '[{0}] {1.name}Tests.robot'.format(index, suite)


This allows you to check valid input test numbers with e.g. if test_index in range(1, len(suites) + 1):, and run a test as suites[test_index - 1].run(). The all ('A'/'a') option is just:

for suite in suites:
    suite.run()


Rather than have the whole system run whenever the script is executed, you should define an entry point function and place it behind a if __name__ == '__main__': guard clause; this allows you to import and test/reuse the functionality elsewhere if required.

You could introduce some more input validation, see e.g. Asking the user for input until they give a valid response for common patterns.

Code Snippets

def run_suite(name):
    print "Running {0} Suite . . .\n".format(name)
    print "=" * 78 + "\n"

    os.chdir("C:/TestCases/Robot/Current/EPRAIS/{0}".format(name))
    print "Moving to dir: %s \n" % os.getcwd()

    ...
class TestSuite(object):

    def __init__(self, name, ...):
        self.name = name
        ... 

    def run(self):
        """Call this method to run the suite."""
        ...

    def _change_working_dir(self):
        """Private methods can hide the implementation detail."""
        os.chdir("C:/TestCases/Robot/Current/EPRAIS/{0.name}".format(self))

    ...
suites = [TestSuite('Login', ...), ...]
for index, suite in enumerate(suites, start=1):
    print '[{0}] {1.name}Tests.robot'.format(index, suite)
for suite in suites:
    suite.run()

Context

StackExchange Code Review Q#157146, answer score: 6

Revisions (0)

No revisions yet.