Advertisement
furas

Python - argparse

May 10th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. '''
  4. $ python run.py -t pierwszy -p iOS
  5. $ python run.py --test pierwszy --platform iOS
  6. '''
  7.  
  8. '''
  9. usage: run.py [-h] [-t TEST] [-p PLATFORM]
  10.               [unittest_args [unittest_args ...]]
  11.  
  12. Pass test and platform
  13.  
  14. positional arguments:
  15.  unittest_args
  16.  
  17. optional arguments:
  18.  -h, --help            show this help message and exit
  19.  -t TEST, --test TEST  test name (default: all)
  20.  -p PLATFORM, --platform PLATFORM
  21.                        platform name (default: Android_7)
  22. '''
  23.  
  24. import argparse
  25.  
  26. def runner():
  27.  
  28.     parser = argparse.ArgumentParser(description='Pass test and platform')
  29.  
  30.     parser.add_argument('-t', '--test', default='all', help='test name (default: all)')
  31.     parser.add_argument('-p', '--platform', default='Android_7', help='platform name (default: Android_7)')
  32.     parser.add_argument('unittest_args', nargs='*')
  33.  
  34.     args = parser.parse_args()
  35.  
  36.     #print('Platform:', args.platform)
  37.     #print('Test:', args.test)
  38.    
  39.     loader = unittest.TestLoader()
  40.  
  41.     if args.test == "all":
  42.         names = loader.discover(start_dir="./tests", pattern="test*.py", top_level_dir=None)
  43.         unittest.TextTestRunner(verbosity=2).run(names)
  44.     elif len(var1) > 1:
  45.         names = loader.discover(start_dir="./tests", pattern=args.test + ".py")
  46.         unittest.TextTestRunner(verbosity=2).run(names)
  47.     else:
  48.         parser.error("wrong test name")
  49.  
  50.     print("test passed into run.py =", args.test)
  51.     print("platform passed into run.py =", args.platform)
  52.    
  53.     return args.platform
  54.    
  55.    
  56. runner()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement