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.
virtualenv
setup.python
manage.py
runserver
. You can explore it by visiting http://localhost:8000
.With these steps, we will see how to programmatically run all the tests in this chapter.
recipe44.py
to contain the code for this recipe.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)
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()
def test_with_robot(): from robot import run run(".")
def test(): test_with_pyccuracy() test_with_robot()
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)
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()
python
recipe44 –test
. In the following screenshot, we can see that all the Pyccuracy tests passed:In the next screenshot, we can see that the Robot Framework tests passed as well:
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
--help
--test
--package
--publish
--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.
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.
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.
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.
18.188.85.135