利用python编写不同环境下都能运行的测试脚本

利用bash来获取当前电脑的环境变量,可以写一个.sh文件,里面获取当前环境,然后在调用python文件执行

  1 # -*- coding: utf-8 -*-
  2 
  3 
  4 import logging
  5 import urllib
  6 import sys
  7 import json
  8 import string
  9 import os
 10 import time
 11 import getopt
 12 import requests
 13 
 14 sys.path.append('../')
 15 
 16 from common.monitor_logging import *
 17 from common.util import *
 18 from common.nagios_status import *
 19 from hongbao_api.baidu_api import *
 20 from common.monitor_config import *
 21 from common.monitor_testcase import *
 22 from common.pinger import *
 23 
 24 
 25 def Usage():
 26     print "exception"
 27 
 28 DEBUG_FLAG=True
 29 ROOT_DIR=''
 30 
 31 def get_user_opt():
 32     global DEBUG_FLAG, ROOT_DIR
 33     if len(sys.argv) > 1:
 34         try:
 35             opts, args = getopt.getopt(sys.argv[1:], 'dr:h')
 36             if len(opts) == 0:
 37                 return
 38 
 39             for opt, arg in opts:
 40                 if opt in ('-h', '--help'):
 41                     Usage()
 42                     sys.exit(3)
 43                 elif opt == '-d':
 44                     DEBUG_FLAG = True
 45                 elif opt == '-r':
 46                     ROOT_DIR = arg
 47 
 48         except getopt.GetoptError:
 49             Usage()
 50             sys.exit(-1)
 51 
 52 
 53 
 54 '''
 55 
 56 '''
 57 class ScoreAuctionDealsUrl():
 58     def __init__(self, testcase_file):
 59         self.config = MonitorConfig()
 60         #获取需要解析的xml的testcase的name
 61         self.testcase = MonitorTestcase(testcase_file)
 62         #日志文件
 63         self.logger = logging.getLogger('baidu')
 64 
 65     #打印返回值和退出
 66     def print_result_and_exit(self, status, msg):
 67         print msg
 68         sys.exit(status)
 69     #检查主机是否联网
 70     def check_host_is_alive(self):
 71         pinger = Pinger()
 72         ret = pinger.check()
 73         return ret
 74 
 75     #开始检测url
 76     def test_all_category(self):
 77         #if not self.check_host_is_alive():
 78         #    msg = 'Network is not availabe'
 79         #    return STATE_UNKNOWN, msg
 80         testcase_name = 'baidu'
 81         url=self.testcase.get_url(testcase_name)
 82         self.logger.debug('testcase=%s' % testcase_name)
 83         self.logger.info('url=%s' % url)
 84 
 85         #请求数据
 86         deals=GetBaiduDeals(url)
 87         #请求json获取返回的状态
 88         status = deals.get_baidu()
 89         print("status:%s"%status)
 90         #一个个遍历:
 91         json_data=deals.json_data
 92         print(json_data)
 93 
 94         return STATE_WARNING, "aaaa"
 95 
 96 
 97 
 98 
 99         if status == 0:
100             msg0 = 'return status is 0'
101             return STATE_WARNING, msg0
102         elif status ==1:
103             msg1 = 'return status is 1'
104             return STATE_WARNING,msg1
105         elif status ==2:
106             msg2 = 'return status is 2'
107             return STATE_CRITICAL,msg2
108         else:
109             msg3='return status is 3'
110             return STATE_UNKNOWN,msg3
111 
112 
113 
114 if __name__ == '__main__':
115 
116       #######################################################
117     get_user_opt()
118     print(DEBUG_FLAG)
119     print(ROOT_DIR)
120     #
121     if DEBUG_FLAG:
122         print 'DEBUG_FLAG is True'
123         log_console_flag = True
124     else:
125         print 'DEBUG_FLAG is False'
126         log_console_flag = False
127 
128     if ROOT_DIR:
129         log_file = ROOT_DIR + '/log/monitor_log.txt'
130     else:
131         log_file= os.getcwd() + '/../../log/monitor_log.txt'
132     log_level = logging.DEBUG
133     set_logging(log_file, log_level, log_console_flag)
134 
135     logger = logging.getLogger("hongbao")
136     print(logger)
137     #
138     ########################################################
139     logger.info('start...')
140     testcase_file = '../../test/hongbao.xml'
141     print(testcase_file)
142     score_auction_deals_url = ScoreAuctionDealsUrl(testcase_file)
143     print(score_auction_deals_url)
144     status, msg = score_auction_deals_url.test_all_category()
145     print(status,msg)
146     #
147     status_str = get_status_str(status)
148     output_msg = '%s, %s' % (status_str, msg)
149     print output_msg
150     exit(status)
#!/bin/sh

_DEBUG=false

# input env
shell_dir=$(cd "$(dirname "$0")"; pwd)
source ${shell_dir}/set_env.sh _DEBUG

ROOT_DIR="$(dirname ${shell_dir})"
_log "ROOR_DIR=${ROOT_DIR}"

#LOG_FILE=${ROOT_DIR}/log/report.log
#_log "LOG FILE=${LOG_FILE}"

cd ${ROOT_DIR}/src/hongbao

python baidu_url.py -r ${ROOT_DIR} $*
exit $?
原文地址:https://www.cnblogs.com/guhao123/p/4045114.html