标签: assertion

对具有时间戳的函数进行 Python 单元测试

在一个函数的单元测试中,该函数输出时间戳并说出主机名以及其他值。在单元测试中,如果我在预期输出中添加一些时间戳和主机名,它将失败,因为每次调用函数时函数的时间戳都会更改,并且每次在不同的计算机中运行时主机名都会更改。解决这个问题的方法是什么?我很感激您的意见。

函数的输出:

{'datetime': '09-10-2018 23:23:23', 'hostname': 'abc.xyz.com',...}
# and it can change every time we run it
Run Code Online (Sandbox Code Playgroud)

但是这个日期时间和主机名当然会根据时间和机器而变化

我想我将在单元测试中使用assertEqual或assertDictEqual的预期输出:

{'datetime': '09-10-2018 23:23:23', 'hostname': 'abc.xyz.com',...}
Run Code Online (Sandbox Code Playgroud)

其他断言检查是否更适合这种情况?

python datetime unit-testing assertion

1
推荐指数
1
解决办法
3402
查看次数

Firebase 登录断言失败:new_time >= Loop->time,文件 c:\ws\deps\uv\src\win\core.c,第 309 行

我买了一台新的 Surface Laptop 3。我通常使用 MAC,但需要 Windows 来执行一些操作。我一直在使用并行,但决定硬着头皮买真正的 Windows 机器。

我使用 firebase 并安装了 firebase-tools 并运行良好。但是,当我尝试登录(使用firebase loginCLI 时),我不断收到如上所示的错误消息:Assertion failed: new_time >= loop->time, file c:\ws\deps\uv\src\win\core.c, line 309。我使用了 bash 和 powershell,结果相同。我还以管理员身份登录到 cmd 提示符。

我搜索了又搜索,发现了一些与时钟相关的东西并进行了检查。我还运行了一些文件系统检查等。但没有任何效果。在我的 Windows 10 环境中,在我的 MAC 上运行一切正常,但这款新笔记本电脑却无法处理。

有人能指出我正确的方向吗?

windows assertion firebase firebase-tools

1
推荐指数
1
解决办法
9514
查看次数

使用 Cypress 验证类似的 JSON 结构?

我得到了这个 JSON 树对象:

let obj1 = {
            "id": 3,
            "estatus": "Abierto",
            "obra": {
                "id": 96,
                "numeroRegistro": "C0001532",
                "ubicacion": {
                    "id": 1,
                    "refDomicilio": "11073",
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我想将它与具有相同结构但不同值的另一个进行比较:

let obj2 = {
            "id": 4,
            "estatus": "Cerrado",
            "obra": {
                "id": 96,
                "numeroRegistro": "C0001532",
                "ubicacion": {
                    "id": 1,
                    "refDomicilio": "11073",
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果我在赛普拉斯上使用此断言,则当值相似时,此断言有效。但是,如果它们不同,那么这个断言就失败了。我只是想比较结构(树),即使值不同。 expect(obj1).to.eql(obj2)

值不同时出错: 预期 { Object (id, estatus, ...) } 深度等于 { Object (id, estatus, ...) }

您知道我可以使用 Cypress 上的哪条指令来执行此操作吗?你知道有什么办法吗?

javascript json unit-testing assertion cypress

1
推荐指数
1
解决办法
2221
查看次数

for 循环与 while 循环中循环不变性的差异

此示例使用的不变量来自https://www.win.tue.nl/~kbuchin/teaching/JBP030/notebooks/loop-invariants.html

我很困惑。示例中的代码使用循环for

我将其翻译为while循环,因为我更好地理解这一点,并且添加了断言来测试不变量。

这个while例子对我来说完全有意义,我可以看到不变量在每个断言点上是如何成立的。

然而,在该for示例中,断言assert total == sum(A[0:i]) and i >= len(A)失败。

我可以理解为什么它可能会影响该i值,因为i在 处停止递增4。但我不确定为什么总和的最终断言需要是assert total == sum(A[0:i + 1])

这似乎是一件非常微妙的事情,围绕着“差一个错误”。我对不变量的“硬编码”也有点不舒服assert total == sum(A[0:0])

while任何人都可以提供我的代码版本到版本的精确转换for,以及不变量的所有必要断言,并解释它们如何/为什么不同?

非常感谢任何帮助。

def my_sum_while(A):
    """
    Loop Invariant: At the start of iteration i of the loop, the variable
    `total` should contain the sum of the numbers from the subarray A[0:i].
    """
    i = 0 …
Run Code Online (Sandbox Code Playgroud)

python assertion loop-invariant

1
推荐指数
1
解决办法
132
查看次数

Visual Studio断言在C++ set comparator上失败

我的代码在我的Visual Studio 2010上有一些问题,但在DevCPP上却没有.在这种情况下,我set在我的代码中使用了C++ STL 来插入,pair<string, double>但后来我希望我set使用值而不是键对它们进行排序,所以我使用自定义比较器来实现这一点.

struct sortPairSecond
{
   bool operator()(const pair<string, double> &lhs, const pair<string, double> &rhs)
   {
    return lhs.second >= rhs.second;
   }
};
Run Code Online (Sandbox Code Playgroud)

代码在DevCPP中工作正常,但在使用VS2010的xtree上遇到调试断言失败.我做了一些调试,我发现错误是由自定义比较器中使用> =引起的,消除=使代码工作但结果不正确,因为我的程序中应该允许重复值.所以任何人都可以帮我解决这个问题?

c++ set visual-studio-2010 assertion

0
推荐指数
1
解决办法
700
查看次数

如果我写assert true或断言false,我看不到影响.为什么?

我在java中面对断言

public static void main(String[] args) {
        assert true;
        assert false;
    }
Run Code Online (Sandbox Code Playgroud)

我不明白我为什么不这样做;在这种情况下看到断言错误?

这段代码片段做了什么?

java assertion

0
推荐指数
1
解决办法
82
查看次数

具有双重条件的Python函数

我想创建一个函数,它返回奇数位置列表元素或列表的负元素.

我的解决方案适用于第一个断言,但第二个生成AssertionError,因为返回[-1,-2,1]而不是[-1,-2].有什么建议?

def solution(input):
  output = []
  for item in input: 
    if item < 0:
        output.append(item)
    elif not item % 2 == 0:
        output.append(item)
  return output

assert solution([0,1,2,3,4,5]) == [1,3,5]
assert solution([1,-1,2,-2]) == [-1,-2]
Run Code Online (Sandbox Code Playgroud)

python assertion

0
推荐指数
1
解决办法
124
查看次数

JMeter无法断言Http响应代码423

我试图在JMeter中声明http响应代码。我认为这确实很简单,但是我遇到了无法解决的问题。

我的服务器可以返回2个响应代码:200和423。200没问题,它可以正常工作,但是我不能断言423,我不知道为什么。

我尝试了以下配置的响应断言:

要测试的字段:响应代码,模式匹配规则:包含

要测试的模式:200-有效423-不起作用200 | 423-200有效,423不起作用(wtf?)

我也尝试了BeanShell Assertion

Failure = !(ResponseCode.contains("200") || ResponseCode.contains("423"));
Run Code Online (Sandbox Code Playgroud)

它也不起作用。我也试图与响应消息匹配以包含“已锁定”-不起作用。服务器响应如下所示:

Thread Name: 10 Users, 100 Repeats 1-10
Sample Start: 2017-05-19 13:06:09 MESZ
Load time: 33
Connect Time: 2
Latency: 33
Size in bytes: 333
Sent bytes:768
Headers size in bytes: 333
Body size in bytes: 0
Sample Count: 1
Error Count: 1
Data type ("text"|"bin"|""): 
Response code: 423
Response message: Locked

Response headers:
HTTP/1.1 423 Locked
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, …
Run Code Online (Sandbox Code Playgroud)

jmeter http-response-codes assertion

0
推荐指数
1
解决办法
703
查看次数

什么是接口断言?

我刚刚在这篇博文中看到了这段代码

type Logger interface {
    Debug(msg string, keyvals ...interface{}) error
    Info(msg string, keyvals ...interface{}) error
    Error(msg string, keyvals ...interface{}) error
}

type tmLogger struct {
    srcLogger kitlog.Logger
}

// Interface assertions
var _ Logger = (*tmLogger)(nil) // What is this?

// ... interface definition ...
Run Code Online (Sandbox Code Playgroud)

什么是"接口断言"?

interface go assertion

0
推荐指数
1
解决办法
1381
查看次数

FluentAssertion - 向if语句添加断言

我有以下Fluent Assertion,我想在if语句中添加.我收到一个错误,说我不能隐式地将类型转换为bool.

我试图明确地转换它但我仍然得到一个错误,说不能将类型转换为bool.

actors.Cast.Should().Contain(actor => actor.Name == "Emilia Clark");

检查上述陈述是否属实的最佳方法是什么?

c# nunit unit-testing assertion fluent-assertions

0
推荐指数
1
解决办法
54
查看次数