I'm writing a suite of web tests in Python using Selenium. Ultimately, I want to generate a file with the results, which will then be sent out in an email. I'm using a main Python script to kick off all of the test scrips, and then the email script afterwards. When a test successfully runs (through the shell), I get a message in the terminal like this:

Ran 1 test in 15.566s


This ^ ^ ^ is what I want written to a file, but after trying multiple methods I can't get it to write the results. I can't figure out where Selenium does this within the Python script.

Anyone have experience with this/have an idea how to do it?

  • 2
    What have you tried that hasn't worked? The first thing that comes to my mind is pipe your output to a file. Dec 3, 2013 at 23:10
  • 1
    Selenium doesn't understand the concept of a "test" -- it's just an API for controlling browsers. Your Python script is relying on some other framework to run the tests. Are you running python with the -m switch?
    – user246
    Dec 3, 2013 at 23:39

1 Answer 1


It seems you may not be accustomed to piping. No worries, it was new to everyone at some point. By using the shell to call your tests, you're mostly there already.

A key thing here is that anything that get's written to your terminal can be "piped" into a file that you name. If you are seeing those results in your terminal, then you can route them to a file.

For example

echo "hello world"

will cause the terminal to print "hello world"

Same example, now piped to a file

echo "hello world" > ./helloworld.txt

will cause "hello world" to be placed inside a file named helloworld.txt (in your working directory, which is what the "./" represents)

Now to get your python script to do this, just do something like:

python my_python_script.py > ./my_test_results.txt

The following examples should work in Windows CMD and Linux Bash shells (not sure which shell you are using). Also, if you don't know your working directory type "cd" in CMD or "pwd" in Bash to have it printed in the terminal. Then go to that folder and find the file.

  • This answer is wrong. Firstly, what is described here is called "redirection", not "piping". Pipes are similar in concept, but are used to connect the output to standard input of another process. (Notice there was no pipe character used in the given example). Secondly, the redirection example given is not going to work here for test output. The question was asking about status output from Python's unittest module. This output is actually written to standard error. Simply redirecting standard output to a file will not capture the status output the OP was looking for. Jan 26, 2018 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.