Let's say I have a Python script file that uses sys.argv
variables. And I want to test the sys.argv
with different values in multiple tests. The issue is that I have to patch and import it multiple times but i think it holds the sys.args
values only from the first import so both TestCases print the same values ['test1', 'Test1']
. Is my approach wrong?
example.py
import sys
ex1 = sys.argv[0]
ex2 = sys.argv[1]
print(ex1)
print(ex2)
test_example.py
import unittest
import mock
import sys
class TestExample(unittest.TestCase):
@mock.patch.object(sys, 'argv', ['test1', 'Test1'])
def test_example1(self):
import example
@mock.patch.object(sys, 'argv', ['test2', 'Test2'])
def test_example2(self):
import example