python2和python3在unittest里的tearDown()使用sys.exc_info()结果不相同

本文通过一个具体的unittest测试案例,探讨了在Python2和Python3中使用sys.exc_info()获取异常信息的不同表现。指出在Python3中,若无异常抛出,该函数返回的将是None。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
 @ Author     :Evan
 @ Date       :2018/11/20 12:25
 @ Version    : 1.0
 @ Description:
 @ Modified By:
"""


import sys
import unittest


class TestOne(unittest.TestCase):

    def setUp(self):
        print("this is setup\n")

    def test_first(self):
        self.assertEqual(1, 2)

    def tearDown(self):
        print("this is tearDown\n")
        print("sys.exc_info()", sys.exc_info())


if __name__ == '__main__':
    unittest.main()

这里面self.assertEqual(1, 2)必定是错误的。

首先我们用Python2执行,结果如下:
在这里插入图片描述
python2中sys.exc_info()是有数据的!

接着我们用Python3执行,结果如下:
在这里插入图片描述
Python3中sys.exc_info()都是None!!

个人能力有限,不知道为什么。

import unittest from time import sleep from selenium.webdriver import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from unittestreport import ddt, yaml_data from config.congfig import Host, PATH from page.login import login @ddt class Sim(unittest.TestCase): def setUp(self): """测试环境初始化""" self.browser = login() self.browser.get(Host) def tearDown(self): """资源清理""" self.browser.quit() @yaml_data(PATH + r'\data\sim.yaml') def test_sim(self, data): # 选择sim管理模块 global result self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div[2]/div[1]/div/ul/div[3]/li/div').click() self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div[2]/div[1]/div/ul/div[3]/li/ul/div[1]/a/li').click() sleep(5) self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[2]/div/form/div[1]/div/div/input').send_keys( data['SN']) self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[2]/div/form/div[4]/div/div/input').send_keys( data['IMSI']) self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[2]/div/form/div[2]/div/div/input').send_keys( data['ICCID']) # 点击查询 self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[2]/div/form/div[5]/div/button/span').click() sleep(6) # 获取结果信息 if data['type'] == "pass": result = self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[5]/div/span[1]').text elif data['type'] == "sz": result = self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[4]/div[3]/table/tbody/tr[1]/td[4]/div').text elif data['type'] == "error": result = self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[4]/div[3]/div/span').text elif data['type'] == "syy": self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[5]/div/button[1]').click() # 定位输入框 input_element = WebDriverWait(self.browser, 10).until( EC.presence_of_element_located( (By.CSS_SELECTOR, 'div.el-pagination__editor input.el-input__inner') ) ) # 等待分页加载完成(根据实际页面元素调整) WebDriverWait(self.browser, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.el-table__body')) ) # 通过 JS 获取输入框的值 result = self.browser.execute_script( "return arguments[0].value", input_element ) elif data['type'] == "xyy": self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[5]/div/button[2]/i').click() # 定位输入框 input_element = WebDriverWait(self.browser, 10).until( EC.presence_of_element_located( (By.CSS_SELECTOR, 'div.el-pagination__editor input.el-input__inner') ) ) # 等待分页加载完成(根据实际页面元素调整) WebDriverWait(self.browser, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.el-table__body')) ) # 通过 JS 获取输入框的值 result = self.browser.execute_script( "return arguments[0].value", input_element ) elif data['type'] == "ymxz": self.browser.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[5]/div/ul/li[3]').click() # 定位输入框 input_element = WebDriverWait(self.browser, 10).until( EC.presence_of_element_located( (By.CSS_SELECTOR, 'div.el-pagination__editor input.el-input__inner') ) ) # 等待分页加载完成(根据实际页面元素调整) WebDriverWait(self.browser, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.el-table__body')) ) # 通过 JS 获取输入框的值 result = self.browser.execute_script( "return arguments[0].value", input_element ) elif data['type'] == "ymsr": try: # 定位输入框 input_element = WebDriverWait(self.browser, 10).until( EC.presence_of_element_located( (By.CSS_SELECTOR, 'div.el-pagination__editor input.el-input__inner') ) ) input_element.send_keys(data['yema']) input_element.send_keys(Keys.RETURN) # 等待分页加载完成(根据实际页面元素调整) WebDriverWait(self.browser, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.el-table__body')) ) # 通过 JS 获取输入框的值 result = self.browser.execute_script( "return arguments[0].value", input_element ) except Exception as e: print(f"操作失败: {e}") self.browser.save_screenshot("error_screenshot.png") # 断言验证 self.assertIn(data['expected'], result, f"断言失败: 预期包含 '{data['expected']}',实际为 '{result}'uoyuo优化·一下代码
最新发布
07-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值