Creating a project-level script to verify this chapter's acceptance tests

We have used pyccuracy_console and pybot to run various test recipes. But management of a Python project involves more than just running tests. Things like packaging, registering with the Python Project Index, and pushing to deployment sites are important procedures to manage.

Building a command-line script to encapsulate all of this is very convenient. With this recipe, we will run a script that runs ALL of the tests covered in this chapter.

Getting ready

  1. We first need to activate our virtualenv setup.
  2. For this recipe, we are using the satchmo shopping cart web application. To start it, switch to the store directory and type python manage.py runserver. You can explore it by visiting http://localhost:8000.
  3. Next, install the Robot Framework and the third-party Selenium plugin, as shown in the earlier recipe Installing the Robot Framework.
  4. This recipe assumes that all of the various recipes from this chapter have been coded.

How to do it...

With these steps, we will see how to programmatically run all the tests in this chapter.

  1. Create a new file called recipe44.py to contain the code for this recipe.
  2. Create a command-line script that defines several options.
    import getopt
    import logging
    import os
    import os.path
    import re
    import sys
    from glob import glob
    
    def usage():
        print
        print "Usage: python recipe44.py [command]"
        print
        print "	--help"
        print "	--test"
        print "	--package"
        print "	--publish"
        print "	--register"
        print
    
    try:
        optlist, args = getopt.getopt(sys.argv[1:],
                "h",
               ["help", "test", "package", "publish", "register"])
    except getopt.GetoptError:
        # print help information and exit:
        print "Invalid command found in %s" % sys.argv
        usage()
        sys.exit(2)
  3. Add a method that starts Selenium, runs the Pyccuracy-based tests, and then shuts down Selenium.
    def test_with_pyccuracy():
        from SeleniumLibrary import start_selenium_server
        from SeleniumLibrary import shut_down_selenium_server
        from time import sleep
    
        f = open("recipe44_selenium_log.txt", "w")
        start_selenium_server(logfile=f)
        sleep(10)
    
        import subprocess
        subprocess.call(["pyccuracy_console"])
    
        shut_down_selenium_server()
        sleep(5)
        f.close()
  4. Add a method that runs the Robot Framework tests.
    def test_with_robot():
        from robot import run
        run(".")
  5. Add a method to run both of these test methods.
    def test():
        test_with_pyccuracy()
        test_with_robot()
  6. Add some stubbed out methods for the other project functions.
    def package():
        print "This is where we can plug in code to run " + 
              "setup.py to generate a bundle."
    
    def publish():
        print "This is where we can plug in code to upload " + 
              "our tarball to S3 or some other download site."
    
    def register():
        print "setup.py has a built in function to " + 
              "'register' a release to PyPI. It's " + 
              "convenient to put a hook in here."
        # os.system("%s setup.py register" % sys.executable)
  7. Add some code that parses the options.
    if len(optlist) == 0:
        usage()
        sys.exit(1)
    
    # Check for help requests, which cause all other
    # options to be ignored.
    for option in optlist:
        if option[0] in ("--help", "-h"):
            usage()
            sys.exit(1)
    
    
    # Parse the arguments, in order
    for option in optlist:
        if option[0] in ("--test"):
            test()
    
        if option[0] in ("--package"):
            package()
    
        if option[0] in ("--publish"):
            publish()
    
        if option[0] in ("--register"):
            register()
  8. Run the script with the testing flag by typing python recipe44 –test. In the following screenshot, we can see that all the Pyccuracy tests passed:
    How to do it...

In the next screenshot, we can see that the Robot Framework tests passed as well:

How to do it...

How it works...

We use Python's getopt module to define command-line options.

    optlist, args = getopt.getopt(sys.argv[1:],
            "h",
           ["help", "test", "package", "publish", "register"])

This maps:

  • "h": -h
  • "help": --help
  • "test": --test
  • "package": --package
  • "publish": --publish
  • "register": --register

We scan the list of received arguments and call the appropriate functions. For our test functions, we used Python's subprocess module to call pyccuracy_console. We could have done the same to call pybot, but Robot Framework provides a convenient API to call it directly.

    from robot import run
    run(".")

This lets us use it inside our code.

There's more...

To run these tests, we need Selenium running. Our Robot Framework tests are built to run Selenium on their own. Pyccuracy doesn't have such a feature, so it needed another means. In those recipes, we used java -jar selenium-server.jar. We could try to manage this, but it is easier to use SeleniumLibrary's API to start and stop Selenium.

This is where writing code in pure Python gives us the most options. We are able to empower Pyccuracy with parts of another library that was never intended to work with it.

Can we only use getopt?

Python 2.7 introduces argparse as an alternative. Current documentation has no indication that getopt is deprecated, so it's safe to use it as we have just done. The getopt module is a nice, easy-to-use command-line parser.

What's wrong with using the various command-line tools?

There is nothing wrong with using tools like pyccuracy_console, pybot, nosetests, and many other tools that come with the Python libraries. The purpose of this recipe is to offer a convenient, alternative approach that brings all these tools into one central script. By investing a little bit of time in this script, we don't have to remember how to use all these features, but instead can develop our script to support the development workflow of our project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.85.135