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
OK
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?
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.Corey Goldberg– Corey Goldberg2018年01月26日 20:45:43 +00:00Commented Jan 26, 2018 at 20:45
-m
switch?