lundi 29 octobre 2018

How to execute an external python script without side effects?

I'm trying to build a simple unit testing framework (called 'cutest') in Python3, which provides some commands for testing and then it runs external test scripts.

The first problem is to run the test scripts independently, without accumulating side effects from one test to another as the tests are executed in a loop.

The second problem is to execute functions (e.g., on_setup()) defined in the external test scripts themselves.

Here is skeleton code for the testing framework ("cutest.py"):

def test(title):
    print("test:", title)
    try:
        on_setup() # function from the current test
    except:
        print("on_setup() not found")

def expect(str):
    print("expect:", str)

def main():
    import glob
    tests = glob.glob("test*.py")
    for script in tests:
        print("*******", script)
        try:
            exec(open(script).read(), globals())
        except:
            print("errors in script:", script)

if __name__ == "__main__":
    main()

And here is "test_1.py" script:

def on_setup():
    print("test_1.py:on_setup")

test("My first test")
expect("foo")

And finally, here is "test_2.py" script:

test("My second test")
expect("bar")

Note that the "test_2.py" does NOT define its on_setup() function.

Now, when I run python cutest.py in the directory with test_1.py and test_2.py, I get the following output:

******* test_1.py
test: My first test
test_1.py:on_setup
expect: foo
******* test_2.py
test: My second test
test_1.py:on_setup
expect: bar

The problem is that the output from test_2.py shows "test_1.py:on_setup" (a side effect from running test_1.py, whereas it should show "on_setup() not found", because on_setup() is not defined in test_2.py.

In my skeleton code "cutest.py", I used the Python exec(.., globals()) call, but perhaps the problems can be solved using __import__() or some other mechanism.

Aucun commentaire:

Enregistrer un commentaire