- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一个用 python 编写的网络服务 ServA
,我想编写一些单元测试。
ServA
做几件事(使用不同的 View ),但所有 View 都会生成类似的请求日志。
这些测试在不同的情况下要检查ServA
的日志,所以这些单元测试有很多重复的代码(日志的结构总是一样的)。
我的想法是编写一个通用函数来避免代码重复,我找到了 this other question这解决了在 unittest 类中创建通用方法的问题。
但是现在如果我有另一个 Web 服务 ServB
和另一组测试并且我需要做同样的事情怎么办?
有没有办法重用泛型函数?
我是否应该像这样简单地创建一个测试类来检查日志:
class MyMetaTestClass(unittest.TestCase):
def check_logs(self, log_list, **kwargs):
#several self.assertEqual
然后 ServA 和 ServB 的测试像这样继承这个类:
class TestServA(MyMetaTestClass):
def test_log1(self):
logs = extract_the_logs()
self.check_logs(logs, log_1=1, log2='foo')
还有其他(更好的)方法吗?
最佳答案
您可以像以前一样从公共(public)基类继承,但基类本身不必是 TestCase 子类 - 您可以将其设为混合类:
# testutils.py
class LogCheckerMixin(object):
""" this class adds log checking abilities to a TestCase.
"""
def check_logs(self, logs, **kw):
self.assertWhatever(something)
# myserver/tests.py
import unittest
from testutils import LogCheckerMixin
class MyServerTest(unittest.TestCase, LogCheckerMixin):
def test_log1(self):
logs = extract_the_logs()
self.check_logs(logs, log_1=1, log2='foo')
或者你可以把它变成一个简单的函数并从你的测试中调用它:
# testutils.py
def check_logs(testcase, logs, **kw):
testcase.assertWhatever(something)
# myserver/tests.py
import unittest
from testutils import check_logs
class MyServerTest(unittest.TestCase):
def test_log1(self):
logs = extract_the_logs()
check_logs(self, logs, log_1=1, log2='foo')
关于Python 单元测试 : having an external function for repetitive code in different modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429557/
在这里,我想要查找的所有字符串都是三个‘a’字符的倍数,但结果是一个‘’的列表,即。[‘’,‘’,‘’]。此语法来自Re文档。我不知道哪里不对,你能帮我吗?。我预计会是[‘AAA’],而结果显然不是。
在这里,我想要查找的所有字符串都是三个‘a’字符的倍数,但结果是一个‘’的列表,即。[‘’,‘’,‘’]。此语法来自Re文档。我不知道哪里不对,你能帮我吗?。我预计会是[‘AAA’],而结果显然不是。
如何在 Java Regex 中定义重复组? 让我们说一个 2 位数字 [0-9]{2} 多次分隔, 12,34,98,11 这是可能的还是 split 的,唯一的机会? 编辑:我喜欢验证和提取。 最
我有一个部门表和一个雇员表。 dept 表与 emp 表与 deptId 存在一对多关系。 当我尝试这段代码时: session.createCriteria(Dept.class).setFetch
我有一个文本文件,其中包含以下形式的信息: A 0 B 1 C 4 D 0 E 1 A 0 B 0 C 2 D 1 E 1 A 1 B 0 C 2 D 0 E 0 ...
这个问题在这里已经有了答案: Replace "\\" with "\" in a string in C# (9 个回答) 关闭 6 年前。 在 C# 应用程序中,我得到桌面文件夹执行此操作: s
我想多次使用 getElemendbyId 函数,只需将 ID 的名称作为变量传递即可。 我想有比以下更优雅的方法: function myFunction2() { document.ge
我正在构建一个交付系统,到目前为止,我的设计看起来像这样: 问题是,我经常需要一个看起来像这样(非常分层)的结构(数组、json、对象...): 这样做的问题是,它会产生大量重复的 StreetAdd
我正在用 java 练习正则表达式,但我遇到了以下行的问题: Pattern pattern = Pattern.compile("\"(.*?)\": {\"detected\": (.*?), \
我正在尝试找出如何使用关键字终止单词的重复。一个例子: class CAQueryLanguage extends JavaTokenParsers { def expression = ("
这两个函数给出了相同的结果,但为什么循环中有“i > 4”而递归中有“size > 5”?为什么数字不同? function branchBoxForLoop(size) { c.strokeSt
我有一段代码,它将接受一个字符串并重复它,使得字符串的长度为x。 >>> import math >>> def repeat(data, length): return (data
我正在尝试使用正则表达式解决问题。我正在使用 Java 正则表达式,它显然类似于 Perl 中的正则表达式。 我想搜索一个字符串,该字符串包含 1-9 中的单个数字,连续重复 3 次或更多次,或者在重
我收到一个我以前从未真正见过的奇怪的 mysql 错误。它是由类似于下面的查询引起的。有什么建议吗? SELECT * FROM table WHERE col REGEXP '^* MAN *
我有以下表格结构。这两个表有很多共同的属性超过 20 我只是列出两个。我也有 10 个与此类似的表。这就是表在数据库中的方式。有超过 10 个具有相似属性的具体表,并且没有以任何方式相互连接。我正在使
当我在做我的一个项目并且不得不使用很多嵌套对象时,当我想从上层对象设置一个深度嵌套对象的实例变量时,我对我在 Java 中的结构设计变得非常不确定.感觉好像我错过了对 Java 结构化设计的基本理解。
我在一个包含字符串的文件中,例如 {?ENV1} {?ENV2} 我想使用 grep 来查找这些使用 grep -o '\{\?\S+?\}' 我的文件 但是我明白了grep: 错误的正则表达式 '\
为什么正则表达式模式/y+?z/匹配整个字符串yyyz? 我的理解是模式 /y+z/ 是贪婪 所以会匹配一个或多个 y 字符后跟一个 z 字符。为了使该表达式不贪婪(仅匹配 y 的第一个实例),我可以
我有一个 PHP/mySQL 搜索脚本,由于某种原因,在开发人员最新更新后,在使用通配符 (*) 搜索时已停止工作。更糟糕的是,它不仅停止工作,而且还返回以下令人讨厌的(对于访问者)mySQL 错误,
我有这个 Regex,但是当尝试在 mysql 查询语句中使用它时,我得到了 SQL Error [1139] [42000]: Got error 'repetition-operator oper
我是一名优秀的程序员,十分优秀!