Last active
September 12, 2019 14:51
-
-
Save GenevieveBuckley/83db81b467cd1a823f50d4293a5f1708 to your computer and use it in GitHub Desktop.
Combining mock patch with pytest's tmpdir fixture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
def bar(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('a') | |
parser.add_argument('b') | |
parser.add_argument('c') | |
args = parser.parse_args() | |
return args | |
def foobar(): | |
args = bar() | |
print(args) | |
return args | |
if __name__ == '__main__': | |
foobar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from gooey import Gooey | |
@Gooey | |
def bar(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('a') | |
parser.add_argument('b') | |
parser.add_argument('c') | |
args = parser.parse_args() | |
return args | |
def foobar(): | |
args = bar() | |
print(args) | |
return args | |
if __name__ == '__main__': | |
foobar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import sys | |
from unittest.mock import patch | |
import pytest | |
import foo | |
def test_foobar(tmpdir): | |
args = argparse.Namespace() | |
args.a = 1 | |
args.b = tmpdir | |
args.c = 3 | |
with patch('foo.bar') as mock_bar: | |
mock_bar.return_value = args | |
result = foo.foobar() | |
assert result.a == args.a | |
assert args.b == tmpdir | |
mock_bar.assert_called | |
def test_main(tmpdir): | |
sys.argv = ['foo.py', '1', tmpdir, '3'] | |
with patch('foo.bar') as mock_bar: | |
args = argparse.Namespace() | |
args.a = 1 | |
args.b = tmpdir | |
args.c = 3 | |
mock_bar.return_value = args | |
os.system('foo.py 1 ' + str(tmpdir) + ' 3') | |
mock_bar.assert_called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import sys | |
from unittest.mock import patch | |
import pytest | |
import goo | |
def test_foobar(tmpdir): | |
args = argparse.Namespace() | |
args.a = 1 | |
args.b = tmpdir | |
args.c = 3 | |
with patch('goo.bar') as mock_bar: | |
mock_bar.return_value = args | |
result = goo.foobar() | |
assert result.a == args.a | |
assert args.b == tmpdir | |
mock_bar.assert_called | |
def test_main(tmpdir): | |
sys.argv = ['goo.py', '1', tmpdir, '3'] | |
with patch('goo.bar') as mock_bar: | |
args = argparse.Namespace() | |
args.a = 1 | |
args.b = tmpdir | |
args.c = 3 | |
mock_bar.return_value = args | |
os.system('goo.py --ignore-gooey 1 ' + str(tmpdir) + ' 3') | |
mock_bar.assert_called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment