python - How to suppress execution of statement in script called with execfile? -


the question posed in title may case of the xy-problem, unable find better concise description. want test number of python scripts running execfile(filename) on each of them, , see whether trigger assertion/throw exception. far good, of them start gui conformation 1 statement, lets world.show('somestring'). automated testing, don't want see gui. how can suppress gui without changing scripts themselves?

edit: regarding comments: in essences, do:

import unittest  class testexamples(unittest.testcase):      def test_firstexample(self):         execfile('example1.py')      def test_secondexample(self):         execfile('example2.py')      # , many more  if __name__ == '__main__':     unittest.main() 

but a) there more two, , prefer not write test function each example. them tested being in folder. possibly solved unittests discover. , b), of them end visualising calculation, matplotlib.pyplot.show(). want suppress visualisation, without changing examples themselves.

here's simple option: assuming have file world.py this...

def show(text):     return some_gui_stuff.confirm(text) 

...then create new file fakeworld.py looks this...

def show(text):     return true 

...then in test script, like...

import sys sys.modules['world'] = __import__('fakeworld') execfile('example1.py') 

when example1.py tries import world, it'll use fake version imported @ top of test script.

this mean you'll have create fake version every function in world.py. if there's lot of functions, 1 or 2 need changing, might easier make test script this...

import world import fakeworld world.show = fakeworld.show execfile('example1.py') 

...or if world.show literally function, don't need create fakeworld.py - like...

import world world.show = lambda x: true execfile('example1.py') 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -