Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active February 23, 2024 15:58
Show Gist options
  • Save mdmitry1/fdc6f814c3d79f9ffe224abe93d1fe67 to your computer and use it in GitHub Desktop.
Save mdmitry1/fdc6f814c3d79f9ffe224abe93d1fe67 to your computer and use it in GitHub Desktop.
Pytest: Calling a program with command line parameters

Pytest: Calling a program with command line parameters

The solution is based on monkeypatch fixture.

In this example myprog reads number from the file myprog_input.txt adds 2 to it and stores result in myprog_output.txt

Environment:

uname -vosri
Linux 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 GNU/Linux
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal
python3.11 --version
Python 3.11.7
https://opensource.org/licenses/MIT
Copyright 2021 Dmitry Messerman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/usr/bin/python3.11
import argparse
import hashlib
def main():
parser = argparse.ArgumentParser(description='myprog')
parser.add_argument('-i')
parser.add_argument('-o')
args = parser.parse_args()
with open(args.i) as f:
input_data=int(f.read())
output_data=input_data+2
f.close()
with open(args.o,"w") as fo:
fo.write(str(output_data) + '\n')
fo.close()
with open(args.o) as fot:
bytes = fot.read().encode() # read entire file as bytes
fot.close()
readable_hash = hashlib.sha256(bytes).hexdigest();
return readable_hash
if __name__ == '__main__':
print(main())
myprog.py -i myprog_input.txt -o myprog_output.txt
f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06
#!/usr/bin/tcsh -f
set script_realpath=`realpath $0`
set script_path=$script_realpath:h
\rm -f myprog_output.txt >& /dev/null
set hash_cmd="${script_path}/myprog.py -i ${script_path}/myprog_input.txt -o myprog_output.txt"
if(1 == `uname | grep ^MINGW | wc -l`) then
set hash_val=`python3.10 $hash_cmd`
unix2dos myprog_output.txt
else
set hash_val=`$hash_cmd`
endif
diff myprog_output.txt ${script_path}/myprog_output_expected.txt
grep -w $hash_val ${script_path}/test_myprog.py
pytest ${script_path}/test_myprog.py
assert myprog.main() == 'f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06'
============================= test session starts ==============================
platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/mdmitry/github/gist/pytest_program
collected 1 item
test_myprog.py . [100%]
============================== 1 passed in 0.04s ===============================
#!/usr/bin/python3.11
import sys
import myprog
def test_myprog(monkeypatch):
with monkeypatch.context() as m:
m.setattr(sys, 'argv', ['myprog', '-i', 'myprog_input.txt', '-o', 'myprog_output.txt'])
assert myprog.main() == 'f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment