有可能从单元测试返回bool吗.测试用例



我希望将测试结果记录在excel文件中。这是我预期的输出

test, True, 2018/11/27    OR    test, False, 2018/11/27

目前,为了实现这一点,我有一个极其繁琐的实现。这是我的Excel编写器类

xlwriter.py

class XLWriter:
def __init__(self, file):
self.workbook = load_workbook(file)
self.worksheet = self.workbook['Sheet1']
self.file = file
def write_true(self, row):
index = 0
# function name, declare test passed, date
values = [inspect.stack()[1][3], True, datetime.date)]
# column numbers of above
columns = [4, 6, 8]
while index < 2:
cell = self.worksheet.cell(row=row, column=columns[index])
cell.value = values[index]
index += 1
def write_true(self, row):
index = 0
# function name, declare test passed, date
values = [inspect.stack()[1][3], False, datetime.date)]
# column numbers of above
columns = [4, 6, 8]
while index < 2:
cell = self.worksheet.cell(row=row, column=columns[index])
cell.value = values[index]
index += 1

并以测试为例

测试.py

try:
self.assertEqual(url, url)
xl.write_true(14)
xl.save()
except:
xl.write_false(14)
xl.save()

这是不可扩展的。我想一个快速的解决方案是根据测试是否成功返回TrueFalse布尔,但我在unittest文档中没有看到这种行为的函数。

简短回答-

长答案-但是。。有办法解决这个问题。


三个月后重新审视这个问题,我们实际计划实现什么?

我们想做以下

  1. 运行单元测试
  2. 告诉我们的数据库测试运行成功

在这种情况下,不需要从测试本身返回任何内容。与其返回布尔值,不如xlwriter.px发送通知,其中包含所需信息。

在此之前,我们需要重写xlwriter.py。自从创建这个脚本以来,我已经将数据库移植到了SQLite。

经理.py

import sqlite3
import datetime
import os
class Manager:
def write_results_true(self, test):
connection = sqlite3.connect(os.path.abspath('path'))
connection.execute('UPDATE Details '
'SET Passed'
'= "True"'
'WHERE Name = "' + test + '";')
connection.commit()
connection.close()
def write_results_false(self, test):
connection = sqlite3.connect(os.path.abspath('path'))
connection.execute('UPDATE Details '
'SET Passed'
'= "False"'
'WHERE Name = "' + test + '";')
connection.commit()
connection.close()

我们已经简化了管理器类,现在,要写入数据库就很简单了。

测试.py

def test_sample_test(self):
"""
A sample test
"""
a = 1
if a == 1:
m.write_results_true(stack()[0].function)
self.assertEqual(a, 1)
else:
m.write_results_false(stack()[0].function)
# A comment on why the test has failed
self.fail('a does not equals 1')

相关内容