Reputation: 4650
I have a Python project with some tests in different folders and files. I wanted to write a script which executes all tests for the project and outputs some results. I want it to be a ruby script (because I know Ruby more than Python and for now I enjoy it more) and I was thinking to get the output from the tests and to parse them with ruby and to output something like "48 tests run in total, all ok" instead of the the output from python.
Long story short - I want I way to get the output from python test_something.py
in a variable or in a file and I want nothing from it on the screen.
Here are my tries:
tests = Dir.glob("**/test_*")
wd = Dir.pwd
output = ''
tests.each do |test|
Dir.chdir(File.dirname(test))
# output += `python #{File.basename(test)}`
# system("python #{File.basename(test)} >> f.txt")
Dir.chdir(wd)
end
I tried both things which are commented, but both of them print the result on the standard exit and in the first one output variable is empty, in the second one the file is created but is empty again :(
Any ideas? Thank you very much in advance! :)
Upvotes: 1
Views: 266
Reputation: 14082
The test framework might have send the result to STDERR
. Try use Open3.capture3
to capture the standard error.
require 'open3'
...
stdout, stderr, status = Open3.capture3(%{python "#{File.basename(test)}"})
and write the standard output and standard error to the destination:
File.write("f.txt", stdout + stderr)
You may check status.success?
to see if you write the external command right. However, the test framework may return non-zero exit code on failed tests. In that case, you should check the stderr
to see the actual error output.
Upvotes: 1
Reputation: 9762
Use Open3.capture2 as below:
output, _ = Open3.capture2("python #{File.basename(test)")
To write output to a file do as below:
File.write("f.txt", output)
Upvotes: 1