如何忽略单元测试中的Python代码块?

如何忽略单元测试中的Python代码块?,python,unit-testing,Python,Unit Testing,我的模块中有以下代码块 PARSER = argparse.ArgumentParser(description='This script gets ELB statistics ' 'and look for any failed instances') PARSER.add_argument('--profile', help='AWS profile - optional (only if multi

我的模块中有以下代码块

PARSER = argparse.ArgumentParser(description='This script gets ELB statistics '
                                             'and look for any failed instances')
PARSER.add_argument('--profile', help='AWS profile - optional (only if multiple '
                                      'accounts are setup in credentials file)', default='')
PARSER.add_argument('--region', help='AWS region. Defaults to ap-southeast-2',
                    default='ap-southeast-2')
PARSER.add_argument('--elb', help='DNS Name of the ELB to test', required=True)
PARSER.add_argument('--start', help='Start time of the load test (YYYY-MM-DD HH:MM:SS)',
                    required=True)
PARSER.add_argument('--end', help='End time of the load test (YYYY-MM-DD HH:MM:SS)',
                    required=True)
PARSER.add_argument('--debug', help='Print debugging information', action='store_true')

ARGS = PARSER.parse_args()

PROFILE = ARGS.profile
REGION = ARGS.region
ELB = ARGS.elb
START_TIME = format_date_string(ARGS.start)
END_TIME = format_date_string(ARGS.end)
DEBUG = ARGS.debug

if (START_TIME and END_TIME) is not None and START_TIME < END_TIME:
    ASG_MON = ASGMonitor(elb_dns_name=ELB, profile_name=PROFILE, region_name=REGION, debug=DEBUG)
    # used not keyword so the script exits with status 0 when function returns True (success)
    exit(not ASG_MON.analyse_elb_for_failed_nodes(START_TIME, END_TIME))
else:
    cprint('Error - Bad start and end date/time input')
    exit(1)
PARSER=argparse.ArgumentParser(description='此脚本获取ELB统计信息'
'并查找任何失败的实例')
PARSER.add_参数('--profile',help='AWS profile-可选(仅当有多个'
“帐户在凭据文件中设置)”,默认值=“”)
PARSER.add_参数('--region',help='AWS region.Defaults为ap-southest-2',
默认值(='ap-东南-2')
add_参数('--elb',help='要测试的elb的DNS名称',required=True)
添加_参数('--start',help='load test的开始时间(YYYY-MM-DD HH:MM:SS)',
必需=真)
添加_参数('--end',help='负载测试的结束时间(YYYY-MM-DD HH:MM:SS)',
必需=真)
add_参数('--debug',help='Print debug information',action='store_true'))
ARGS=PARSER.parse_ARGS()
PROFILE=ARGS.PROFILE
REGION=ARGS.REGION
ELB=ARGS.ELB
开始时间=格式日期字符串(ARGS.START)
结束时间=格式日期字符串(ARGS.END)
DEBUG=ARGS.DEBUG
如果(开始时间和结束时间)不是无且开始时间<结束时间:
ASG\u MON=ASGMonitor(elb\u dns\u name=elb,profile\u name=profile,region\u name=region,debug=debug)
#使用not关键字,因此当函数返回True(成功)时脚本将以状态0退出
退出(非ASG\u MON.分析\u失败的\u节点的\u elb\u(开始时间、结束时间))
其他:
cprint('错误-错误的开始和结束日期/时间输入')
出口(1)
我只想在单元测试中包括ASGMonitor类(在同一个文件中)。但是argparse会导致我的测试出现问题

py试验——cov elb_监测试验——cov在80以下不合格——cov报告 缺项

我发现了错误

========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 2.7.12, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /Users/achinthag/Documents/Git_Workspace/ea-gatling/elb_monitoring, inifile: 
plugins: cov-2.4.0
collected 0 items / 1 errors 


---------- coverage: platform darwin, python 2.7.12-final-0 ----------
Name                                Stmts   Miss  Cover   Missing
-----------------------------------------------------------------
src/elb_monitoring/__init__.py          0      0   100%
src/elb_monitoring/elb_monitor.py      87     65    25%   15-17, 22-26, 35-49, 53-55, 61-70, 74-90, 94-111, 129-142
-----------------------------------------------------------------
TOTAL                                  87     65    25%

================================================================================================= ERRORS =================================================================================================
_______________________________________________________________________________ ERROR collecting test/test_elb_monitor.py ________________________________________________________________________________
test/test_elb_monitor.py:3: in <module>
    from elb_monitoring.elb_monitor import *
src/elb_monitoring/elb_monitor.py:127: in <module>
    ARGS = PARSER.parse_args()
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py:1701: in parse_args
    args, argv = self.parse_known_args(args, namespace)
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py:1733: in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py:1957: in _parse_known_args
    self.error(_('argument %s is required') % name)
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py:2374: in error
    self.exit(2, _('%s: error: %s\n') % (self.prog, message))
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py:2362: in exit
    _sys.exit(status)
E   SystemExit: 2
-------------------------------------------------------------------------------------------- Captured stderr ---------------------------------------------------------------------------------------------
usage: py.test [-h] [--profile PROFILE] [--region REGION] --elb ELB --start
               START --end END [--debug]
py.test: error: argument --elb is required
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================================== 1 error in 0.61 seconds =========================================================================================
==========================================================================================================================================测试会话开始===========================================================================================
平台darwin——Python 2.7.12、pytest-3.0.6、py-1.4.32、Plugy-0.4.0
rootdir:/Users/achithag/Documents/Git_Workspace/ea gatling/elb_monitoring,INI文件:
插件:cov-2.4.0
已收集0项/1个错误
----------覆盖范围:平台darwin,python 2.7.12-final-0----------
姓名Stmts未找到封面
-----------------------------------------------------------------
src/elb_监控/_初始__.py 0 100%
src/elb_监控/elb_监控。py 87 65 25%15-17、22-26、35-49、53-55、61-70、74-90、94-111、129-142
-----------------------------------------------------------------
总数87 65 25%
======================================================================================================================================================错误=================================================================================================
_______________________________________________________________________________收集test/test\u elb\u monitor.py时出错________________________________________________________________________________
测试/测试elb_监视器。py:3:in
从elb_monitoring.elb_monitor导入*
src/elb_monitoring/elb_monitoring.py:127:in
ARGS=PARSER.parse_ARGS()
/usr/local/ceral/python/2.7.12_2/Frameworks/python.framework/Versions/2.7/lib/python2.7/argparse.py:1701:in parse_args
args,argv=self.parse\u known\u args(args,名称空间)
/usr/local/ceral/python/2.7.12_2/Frameworks/python.framework/Versions/2.7/lib/python2.7/argparse.py:1733:in parse_known_args
名称空间,args=self.\u解析\u已知\u args(args,名称空间)
/usr/local/ceral/python/2.7.12_2/Frameworks/python.framework/Versions/2.7/lib/python2.7/argparse.py:1957:in(解析)已知的参数
self.error(389;('参数%s是必需的')%name)
/usr/local/ceral/python/2.7.12_2/Frameworks/python.framework/Versions/2.7/lib/python2.7/argparse.py:2374:错误
self.exit(2,(“%s:错误:%s\n')%(self.prog,消息))
/usr/local/ceral/python/2.7.12_2/Frameworks/python.framework/Versions/2.7/lib/python2.7/argparse.py:2362:退出
_系统退出(状态)
E系统出口:2
--------------------------------------------------------------------------------------------俘获stderr---------------------------------------------------------------------------------------------
用法:py.test[-h][--profile-profile][--region-region]--elb-elb--start
开始--结束[--调试]
py.test:错误:参数--elb是必需的
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 中断:收集过程中出现1个错误!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================================================================================================================0.61秒内出现1个错误=========================================================================================
如何忽略测试中的这段代码


谢谢,

您可以将解析器参数提取为助手方法,并相应地进行处理

def parse_args(args):
    PARSER = argparse.ArgumentParser(description='This script gets ELB statistics '
                                                 'and look for any failed instances')
    PARSER.add_argument('--profile', help='AWS profile - optional (only if multiple '
                                          'accounts are setup in credentials file)', default='')
    PARSER.add_argument('--region', help='AWS region. Defaults to ap-southeast-2',
                        default='ap-southeast-2')
    PARSER.add_argument('--elb', help='DNS Name of the ELB to test', required=True)
    PARSER.add_argument('--start', help='Start time of the load test (YYYY-MM-DD HH:MM:SS)',
                        required=True)
    PARSER.add_argument('--end', help='End time of the load test (YYYY-MM-DD HH:MM:SS)',
                        required=True)
    PARSER.add_argument('--debug', help='Print debugging information', action='store_true')

    parsed_args = PARSER.parse_args()
    return parsed_args

def mymethod(ARGS):
    PROFILE = ARGS.profile
    REGION = ARGS.region
    ELB = ARGS.elb
    START_TIME = format_date_string(ARGS.start)
    END_TIME = format_date_string(ARGS.end)
    DEBUG = ARGS.debug

    if (START_TIME and END_TIME) is not None and START_TIME < END_TIME:
        ASG_MON = ASGMonitor(elb_dns_name=ELB, profile_name=PROFILE, region_name=REGION, debug=DEBUG)
        # used not keyword so the script exits with status 0 when function returns True (success)
        exit(not ASG_MON.analyse_elb_for_failed_nodes(START_TIME, END_TIME))
    else:
        cprint('Error - Bad start and end date/time input')
        exit(1)

args = parse_args(sys.argv[1:])
mymethod(args)
def parse_args(args):
PARSER=argparse.ArgumentParser(description='此脚本获取ELB统计信息'
'并查找任何失败的实例')
PARSER.add_参数('--profile',help='AWS profile-可选(仅当有多个'
“帐户在凭据文件中设置)”,默认值=“”)
PARSER.add_参数('--region',help='AWS region.Defaults为ap-southest-2',
默认值(='ap-东南-2')
add_参数('--elb',help='要测试的elb的DNS名称',required=True)
添加_参数('--start',help='load test的开始时间(YYYY-MM-DD HH:MM:SS)',
必需=真)
添加_参数('--end',help='负载测试的结束时间(YYYY-MM-DD HH:MM:SS)',
必需=真)
作语法分析
# everything that needs to be accessed when importing
# for instance, your
class ASGMonitor(object):
    # your stuff here

# Th never put your main program code directly
# in the module, create a function for this:
def main():
    # put code that must not be run when importing here

# Step 2 : only run main if your module is run directly
# but not if it is imported:
if __name__ == '__main__':
    main()