1

I would like to suppress output using print from a tested function, while keeping output from the testing function, using a decorator on the testing function, in a safe manner. Essentially:

script.py

def func():
    print("script")

This module is called in:

test_script.py

import script

@some_decorator
def test_func():
    print("test_script")
    script.func()

test_func()

What should some_decorator be, for test_script to print without script? I can import any library as necessary on either of the scripts, preferably from the standard library.

Thanks!

EDIT: I'm using importlib to import script.py, so the import line should be something like this:

import importlib
file_name = "script"
script = importlib.import_module(file_name)
Melvin
  • 1,530
  • 11
  • 18
  • You'll have to write it yourself, there's nothing in the standard library. – Peter Wood Jan 16 '18 at 09:33
  • The clean - and recommended - way to achieve this is to use the `logging` package in place of "print"ing. The standard documentation provides a tutorial with plenty of examples. – glenfant Jan 16 '18 at 09:39
  • Here is what you need, so it's possible duplicate: https://stackoverflow.com/questions/12998908/is-it-possible-to-mock-pythons-built-in-print-function – Sergius Jan 16 '18 at 09:45
  • @Sergius Here you want to mock `print` in the SUT but not in the test itself, which I guess will be tricky. – Stop harming Monica Jan 16 '18 at 10:03

0 Answers0