是否有一个图书馆的鼻子友好断言的事情,如会员和身份(例如assert_contains(x, y),assert_is(a, b))?
任务:
- 给定:图像文件名列表
- 待办事项:创建一个文件名不包含单词"thumb"的新列表 - 即仅定位非缩略图图像(使用PIL - Python Imaging Library).
我试过r".*(?!thumb).*"但是失败了.
我已经找到了解决方案(在这里的stackoverflow)来预先添加^到正则表达式并将其置于.*负向前瞻:r"^(?!.*thumb).*"这现在有效.
问题是,我想了解为什么我的第一个解决方案不起作用,但我不这样做.由于正则表达式足够复杂,我真的很想理解它们.
我所理解的是^告诉解析器以下条件是在字符串的开头匹配.但不是.*(不工作)第一个例子也不是从字符串的开头开始的吗?我认为它会从字符串的开头开始,并在到达"拇指"之前搜索尽可能多的字符.如果是这样,它将返回不匹配.
有人可以解释为什么r".*(?!thumb).*"不起作用但是r"^(?!.*thumb).*"吗?
谢谢!
我熟悉标准C++断言是如何工作的.这在我的项目中运行良好,可用于各种测试目的.
例如,假设我想检查我的代码是否抛出了某个异常.
如果不使用像CPPUnit这样的测试框架,这可能吗?
我想知道在哪种情况下我可以使用"主样本"或"子样本"或两者同时用于响应断言中的文本响应.
我用谷歌搜索了它,但还没有得到满意的答案.
帮助赞赏.
我遇到了OpenCV程序的问题,该程序正确编译但是当它运行时中止发出此错误:
OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in locateROI, file /home/user/Desktop/HOME/src/OpenCV/modules/core/src/matrix.cpp, line 647
terminate called after throwing an instance of 'cv::Exception'
what(): /home/user/Desktop/HOME/src/OpenCV/modules/core/src/matrix.cpp:647: error: (-215) dims <= 2 && step[0] > 0 in function locateROI
Aborted
Run Code Online (Sandbox Code Playgroud)
我正在使用ubuntu和最新的OpenCV库,但它在使用Eclipse的Leopard上也给出了同样的错误.
matrix.cpp文件位于我的OpenCV源文件夹中.
我无法理解为什么我会收到此错误.
你能帮我吗?
非常感谢.
我怎样才能编写一个自定义断言,就像assertFoo($expected, $actual)错误"堆栈跟踪"的内置断言一样?
我目前定义了以下方法(在扩展的类中PHPUnit_Framework_TestCase):
public static function assertFoo($expected, $actual) {
self::assertEquals($expected, $actual);
}
Run Code Online (Sandbox Code Playgroud)
如果我从测试中调用它并且测试失败,我在调用堆栈中得到两个项目:
1) PreferencesTest::testSignupTeacher
Failed asserting that 5 matches expected 3.
/vagrant/myproject/tests/integration/PreferencesTest.php:17
/vagrant/myproject/tests/integration/PreferencesTest.php:136
Run Code Online (Sandbox Code Playgroud)
第17行是assertFoo()调用内置函数assertEquals()并失败的地方; 第136行assertFoo()被称为.
如果我改变测试assertEquals()直接调用,我只得到一个:
1) PreferencesTest::testSignupTeacher
Failed asserting that 3 is true.
/vagrant/myproject/tests/integration/PreferencesTest.php:136
Run Code Online (Sandbox Code Playgroud)
在我的在线Java编程类中,我必须编写一个用户输入其年龄的程序,并检查它是否在0到125之间 - 如果不是,则显示错误代码,我需要使用断言来执行此操作.这是我的代码:
import java.util.Scanner;
public class eproject1 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("What is your age? ");
int num = input.nextInt();
System.out.println(num);
assert(num >= 1 && num < 125);
System.out.printf("You entered %d\n", num); // NEED TO FIX, SOMETHING ISN'T RIGHT
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于它告诉你你输入了什么年龄,即使它超出了1到124的范围.研究说我需要实际启用断言,但我在网上找到的结果非常无益,因为它们涉及一些Eclipse的东西 - 我我确定这是一种新手的错误,因为我不擅长编程.
那么......你如何让断言的东西起作用?
在科特林与JUnit5我们可以使用assertFailsWith
在带有 JUnit5 的 Java 中,您可以使用assertThrows
在 Java 中,如果我想将可执行文件的声明与执行本身分开,为了以 Given-Then-When 形式阐明测试,我们可以assertThrows像这样使用 JUnit5 :
@Test
@DisplayName("display() with wrong argument command should fail" )
void displayWithWrongArgument() {
// Given a wrong argument
String arg = "FAKE_ID"
// When we call display() with the wrong argument
Executable exec = () -> sut.display(arg);
// Then it should throw an IllegalArgumentException
assertThrows(IllegalArgumentException.class, exec);
}
Run Code Online (Sandbox Code Playgroud)
在 Kotlin 中,我们可以使用assertFailsWith:
@Test
fun `display() with wrong argument command should fail`() { …Run Code Online (Sandbox Code Playgroud) TypeScript 具有is有助于创建用于类型检查的测试函数的运算符。最近我看到这个运算符的两种不同实现,其中之一使用asserts关键字。
我在文档中没有找到有关两种使用方式差异的信息。我玩了一下它,如果我是正确的,asserts不会让你从函数返回任何东西,但除此之外我没有发现任何差异。
这是我测试过的代码:
// Asserts and tests the value without returninng anything
function assertIsString(value: unknown): asserts value is string {
if (typeof value !== "string") throw Error("value is not a string");
}
// Tests the value and returns something so it can be used for typecheck
// more explicitly
function testIsString(value: unknown): value is string {
return typeof value === "string";
}
const string = "hello";
const number = 123;
assertIsString(string); // does …Run Code Online (Sandbox Code Playgroud) 在我看来,由于页面设计不佳,我发现自己在验证页面上一个或多个元素的可见性或不存在时遇到问题。
问题是有些元素不存在,而有些元素具有 CSS 属性 display:none。但现有的测试代码会检查 not.exist,这会导致测试失败。但我不能更改为 not.be.visible,因为那样它会在其他元素上失败。
那么:是否可以在断言中执行 OR 操作?类似的东西
cy.get('blabla').should('not.be.visible').or.cy.get('blabla').should('not.exist');
Run Code Online (Sandbox Code Playgroud)
上面的行编译,但在第二部分产生未定义,因此它不起作用。
这是代码:
(我不认为代码架构很重要——问题基本上是“或”的问题。)
page.sjekkAtDellaanFelterVises(2, 2, [
DellaanFelter.formaal,
DellaanFelter.opprinneligLaanebelop,
DellaanFelter.utbetalingsdato,
DellaanFelter.restlaanInnfridd,
]);
public sjekkAtDellaanFelterVisesALT(sakRad: number, delLanRad: number, felter: DellaanFelter[]) {
this.sjekkFelter(felter, DellaanFelter, (felt: string) => this.delLanAccordionBody(sakRad, delLanRad).get(this.e2e(felt)));
}
@ts-ignore
public sjekkFelterALT<T, E extends Node = HTMLElement>(felter: T[], enumType, lookupFn: (felt: string) => Chainable<JQuery<E>>) {
this.valuesOfEnum(enumType).forEach(felt => {
this.sjekkFelt(felt, felter, enumType, lookupFn);
});
}
// @ts-ignore enumType fungerer fint i praksis ...
public sjekkFeltALT<T, E extends Node = HTMLElement>(felt: string, felter: …Run Code Online (Sandbox Code Playgroud)