1

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! :)

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Faery glad to see it helps
1

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)

4 Comments

It's the same again :( output variable is empty each time, so the file is empty, too and I see the result on the standart exit :( I execute this with ruby name_of_file.rb
Open3.capture2 captures standard output, which is the same as the back quote. If @Faery cannot capture anything with back quote, Open.capture2 won't work either.
@ArieShaw it is not the same as backticks as it returns a Process::Status object which backticks doesn't, the difference between this and your answer is that you are capturing stderr
@bjhaid You threw status away.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.