Skip to content

Commit 55e72b2

Browse files
committed
to add argparse test script
1 parent 0f302ab commit 55e72b2

File tree

5 files changed

+62
-50
lines changed

5 files changed

+62
-50
lines changed

case/argparse/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _temp_dir():
1717
''' _temp_dir '''
1818
temp = tempfile.mkdtemp(prefix='argprase-')
1919
print('\n')
20-
print('the current temp dir is {}'.format(temp))
20+
logging.debug('the current temp dir is {}'.format(temp))
2121
yield temp
2222
shutil.rmtree(temp)
2323

@@ -27,6 +27,6 @@ def _temp_file():
2727
''' _temp_file '''
2828
fd, temp = tempfile.mkstemp(prefix='argprase-', suffix='.tmp')
2929
# print('\n')
30-
print('the current temp path is {}'.format(temp))
30+
logging.debug('the current temp path is {}'.format(temp))
3131
yield temp
3232
os.remove(temp)
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
# -*- coding:utf-8 -*-
22

33
import argparse
4-
import os
5-
import pytest
6-
7-
8-
def test_cmd_1(execute):
9-
''' the test cmd '''
10-
print('\ntest_cmd_1: {}'.format(execute))
11-
print('the test_cmd_1 ended.')
12-
# pytest -v -s ./case/argparse/test_argparse.py
13-
# pytest -v -s test_argparse.py
14-
pass
15-
16-
17-
@pytest.fixture(scope='session', params=['a', 'b', pytest.param('c', marks=pytest.mark.skip)])
18-
def execute(_temp_dir, _temp_file, request):
19-
''' the execute '''
20-
value = request.param
21-
print('\nexecute: {}'.format(value))
22-
return value
234

245

256
def function_argparse():
@@ -54,8 +35,4 @@ def function_argparse():
5435

5536
if __name__ == "__main__":
5637
''' the main point '''
57-
# function_argparse()
58-
retcode = pytest.main(args=['-v', '-s', os.path.abspath(__file__)])
59-
# retcode = pytest.main(args=['--fixtures', os.path.abspath(__file__)])
60-
# retcode = pytest.main(args=['--collect-only', os.path.abspath(__file__)])
61-
print(retcode)
38+
function_argparse()

case/argparse/test_argparse_1.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
# -*- coding:utf-8 -*-
22

33
import argparse
4-
import os
5-
import subprocess
6-
import pytest
74
import sys
85

96

10-
def test_cmd_1(execute):
11-
''' the test cmd '''
12-
print('the test_cmd_1 ended.')
13-
# pytest -v -s ./case/argparse/test_argparse_1.py
14-
# pytest -v -s test_argparse_1.py
15-
pass
16-
17-
18-
@pytest.fixture()
19-
def execute(_temp_dir, _temp_file):
20-
try:
21-
args = [sys.executable, __file__, '1']
22-
# args = [sys.executable, __file__, '-h']
23-
subprocess.check_output(args=args, )
24-
subprocess.check_call(args=args, )
25-
finally:
26-
print('the execute...')
27-
28-
297
def function_argparse():
308
#
319
# the command line srcipt:
@@ -62,4 +40,3 @@ def function_argparse():
6240
if __name__ == "__main__":
6341
''' the main point '''
6442
function_argparse()
65-
# pytest.main(args=['-v', '-s', os.path.abspath(__file__)])

case/argparse/test_script.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import logging
4+
import os
5+
import subprocess
6+
import pytest
7+
import sys
8+
9+
10+
def test_cmd_1(execute):
11+
''' the test cmd '''
12+
print('\n the test_cmd_1 ended.')
13+
logging.info('the test_cmd_1 ended.')
14+
15+
16+
# @pytest.fixture(scope='session', params=['test_argparse_0.py'])
17+
@pytest.fixture(scope='session', params=['test_argparse_1.py'])
18+
# @pytest.fixture(scope='session', params=['test_argparse_0.py', 'test_argparse_1.py'])
19+
def generic_cmd_prefix(_temp_dir, _temp_file, request):
20+
''' the generic command prefix '''
21+
dest_script_name = os.path.join(os.path.dirname(__file__), request.param)
22+
logging.debug('the generic_cmd_prefix dest_script_name is {}'.format(dest_script_name))
23+
return [sys.executable, dest_script_name]
24+
25+
26+
@pytest.fixture(params=[['1'], ['-h']])
27+
def generic_cmd_args(generic_cmd_prefix, request):
28+
''' the generic command args '''
29+
dest_script_args = generic_cmd_prefix + request.param
30+
logging.debug('the current generic_cmd_args dest_script_args: {}'.format(dest_script_args))
31+
return dest_script_args
32+
33+
34+
@pytest.fixture()
35+
def execute(generic_cmd_args):
36+
try:
37+
args = generic_cmd_args
38+
logging.warning('the parameters required for current execution, the command script: [\"{}\"]'.format(
39+
' '.join(args)))
40+
std_output = subprocess.check_output(args)
41+
if type(std_output) == bytes:
42+
# logging.warning(str(std_output, encoding='utf-8'))
43+
logging.info('the capture std output from subprocess:\n {}'.format(str(std_output, encoding='utf-8')))
44+
else:
45+
logging.warning(std_output)
46+
retcode = subprocess.check_call(args)
47+
if retcode == 0:
48+
logging.info('the script verify passed.')
49+
finally:
50+
logging.debug('the execute ended')
51+
52+
53+
if __name__ == "__main__":
54+
''' the main point '''
55+
retcode = pytest.main(args=['-v', '-s', os.path.abspath(__file__)])
56+
# retcode = pytest.main(args=['--fixtures', os.path.abspath(__file__)])
57+
# retcode = pytest.main(args=['--collect-only', os.path.abspath(__file__)])
58+
print(retcode)

case/pytest/test_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66

7-
@pytest.fixture(scope="module", params=["mod1", "mod2"])
7+
@pytest.fixture(scope="module", params=["mod1", "mod2", pytest.param('mod3', marks=pytest.mark.skip)])
88
def modarg(request):
99
''' modarg '''
1010
param = request.param

0 commit comments

Comments
 (0)