Selenium Python testing: would like to write test results to a file



  • 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?



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2