有没有像 C/CPP 中那样使用 pythonic 方式编写带有消息的断言:
assert(i <= j && "more participants than medals");
Run Code Online (Sandbox Code Playgroud)
当我尝试等效方法时,我收到 pylint 错误,这可能表明有更好的方法(?):
R1726: Boolean condition 'i <= j and "..."' may be simplified to 'i <= j' (simplifiable-condition)
Run Code Online (Sandbox Code Playgroud) 我正在用 c 编写一个名为:“shell”(我模仿 shell)的程序,我想编写一些测试以确保我遵循所有测试用例,所以我尝试使用#include <assert.h>
但我不明白如何模仿终端中的用户输入。我尝试使用包含文本的文件并更改stdin为该输入文件并重定向stdout到输出文件,但它不起作用。
我还尝试使用该函数将输入插入到终端system(),但效果不佳。
shell 程序如何运行的示例
所以shell.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
if (strncmp(buf, "quit", 4) == 0)
{
exit(0);
}
int fildes[2];
pipe(fildes);
if (fork() == 0)
{
close(fildes[0]);
dup2(fildes[1], STDOUT_FILENO);
execlp("ls", "ls", "-l", NULL);
perror("exec error");
exit(1);
}
else
{
close(fildes[1]);
read(fildes[0], buf, 1024);
printf("%s", buf);
}
}
return 0; …Run Code Online (Sandbox Code Playgroud) public void foo()
{
assert false; /* Line 5 */
assert true; /* Line 6 */
}
Run Code Online (Sandbox Code Playgroud)
断言在这做什么?我熟悉断言陈述
运行时执行的两种情况有什么区别?
private void doStuff() {
assert (y > x);
// more code assuming y is greater than x
}
private void doStuff() {
assert (y > x): "y is " + y + " x is " + x;
// more code assuming y is greater than x
}
Run Code Online (Sandbox Code Playgroud) 我试图看看不同语言如何处理浮点数.我知道浮点表示有一些固有的问题,这就是为什么如果你在Python中做0.3 + 0.6,你得到0.899999而不是0.9
然而,这些代码片段让我感到震惊:
double x = 0.1,
sum = 0;
for(int i=0; i<10; ++i)
sum += x;
printf("%.9lf\n",sum);
assert(sum == 1.0);
Run Code Online (Sandbox Code Playgroud)
上面的代码片段工作正常.它打印1.0.但是,以下代码段会给出运行时错误:
double x = 0.1,
sum = 0;
for(int i=0; i<10; ++i)
sum += x;
assert(sum == 1.0);
printf("%.9lf\n",sum);
Run Code Online (Sandbox Code Playgroud)
上面两个片段中唯一的变化是assert和printf语句的顺序.这让我觉得printf以某种方式修改了它的参数并以某种方式将它们四舍五入.
有人可以对此有所了解吗?
所以我试图导入一个模块,并从该模块中的类测试方法。
这是方法的示例。
def production_warning(self, targetenv):
if targetenv == 'prdv':
prodwarning1 = raw_input("WARNING: You are deploying to the production environment. Are you sure you want to do this? Y/N: ").upper()
if prodwarning1 == "N":
sys.exit()
prodwarning2 = raw_input("DEPLOYING TO PRDV, ARE YOU REALLY, REALLY SURE? Y/N: ").upper()
if prodwarning2 == "N":
sys.exit()
else:
return True
Run Code Online (Sandbox Code Playgroud)
这是我尝试编写的测试示例。
def production_warning():
try:
assert test.production_warning('prdv') is not errors
assert test.validate_params('fakeenv') is errors
print "Test Passed {0}/5: validate_params".format(counter)
test_db_refresh()
except:
print "Test Failed {0}/5: validate_params".format(counter)
test_db_refresh() …Run Code Online (Sandbox Code Playgroud) 我创建了一个返回 200 ok 响应的 Web API。
public IHttpActionResult get()
{
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
此外,我使用 NUnit 框架创建了一个测试项目。
var controller = new StatusController();
var result= controller.level0() as OkNegotiatedContentResult<object>;
IHttpActionResult actionResult = controller.level0();
Assert.AreEqual(HttpStatusCode.OK, actionResult);
Run Code Online (Sandbox Code Playgroud)
但我有这样的错误
Expected: OK
But was: <System.Web.Http.Results.OkResult>
Run Code Online (Sandbox Code Playgroud)
当我尝试调试“actionResult”变量时,它包含一个错误
Request = '((System.Web.Http.Results.OkResult)actionResult).Request' threw an exception of type 'System.InvalidOperationException'
Run Code Online (Sandbox Code Playgroud)
如何检查我的 http 状态代码?
我经常使用 testNG 的 assertTrue 来验证交易是否正确
public void verifyAmount(WebElement element, String someText){
assertTrue(element.getText().contains(someText));
}
Run Code Online (Sandbox Code Playgroud)
当它失败时,它说
java.lang.AssertionError: did not expect to find [true] but found[false]
Run Code Online (Sandbox Code Playgroud)
是否可以更改断言错误以说明究竟出了什么问题,而不仅仅是真/假陈述?是否可以使该消息更具体?有没有办法让断言错误说:
java.lang.AssertionError: did not expect to find [10.000 $] but found[3000 $]
Run Code Online (Sandbox Code Playgroud) 我有这个练习,其中我们使用模板类尝试右值和左值引用的各种组合,我收到两个断言错误;如果有人可以指导的话。
#include <assert.h>
typedef int& IntLRef;
typedef IntLRef& IntLLRef;
typedef IntLRef&& IntLRRef;
typedef int&& IntRRef;
typedef IntRRef& IntRLRef;
typedef IntRRef&& IntRRRef;
template<typename T, typename U>
struct IsSameType
{
static const bool value = false;
};
template<typename T>
struct IsSameType <T, T>
{
static const bool value = true;
};
static_assert(IsSameType<IntLRef, IntLLRef>::value, "LRef DIF LLRef"); static_assert(IsSameType<IntLRef, IntLRRef>::value, "LRef DIF LRRef"); static_assert(IsSameType<IntLLRef, IntLRRef>::value, "LLRef DIF LRRef");
static_assert(IsSameType<IntRRef, IntRLRef>::value, "RRef DIF RLRef"); static_assert(IsSameType<IntRRef, IntRRRef>::value, "RRef DIF RRRef"); static_assert(IsSameType<IntRLRef, IntRRRef>::value, "RLRef DIF RRRef");
int …Run Code Online (Sandbox Code Playgroud) 代码如下:
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ContactPageElements {
public static WebElement element = null;
public static String baseURL1 = "http://something.com";
//Clicking logo should take you back to the baseURL
public static void clickLogo (WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id='blah'"));
element.click();
String currentURL = driver.getCurrentUrl();
assert.assertEquals(currentURL, baseURL1);
}
}
Run Code Online (Sandbox Code Playgroud)
对于断言,我收到错误:“类型不匹配:无法从 void 转换为布尔值”
我试图了解(以我的 C# 背景)以下断言如何工作msf_s:
https://github.com/ShiftMediaProject/libcdio/blob/master/lib/driver/sector.c#L66
cdio_assert (msf != 0);
Run Code Online (Sandbox Code Playgroud)
https://github.com/ShiftMediaProject/libcdio/blob/master/lib/driver/cdio_assert.h#L50
#define cdio_assert(expr) \
assert(expr)
Run Code Online (Sandbox Code Playgroud)
https://github.com/ShiftMediaProject/libcdio/blob/master/include/cdio/types.h#L217
PRAGMA_BEGIN_PACKED
struct msf_s {
uint8_t m, s, f; /* BCD encoded! */
} GNUC_PACKED;
PRAGMA_END_PACKED
Run Code Online (Sandbox Code Playgroud)
它用于以下代码段:
lba_t
cdio_msf_to_lba (const msf_t *msf)
{
uint32_t lba = 0;
cdio_assert (msf != 0);
lba = cdio_from_bcd8 (msf->m);
lba *= CDIO_CD_SECS_PER_MIN;
lba += cdio_from_bcd8 (msf->s);
lba *= CDIO_CD_FRAMES_PER_SEC;
lba += cdio_from_bcd8 (msf->f);
return lba;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要在 C# 中复制相同的行为。
题:
是否cdio_assert总结每个字段msf_s并断言它们不等于零?
在'release'版本中是否禁用了断言?
如何可选的标志一样-O0,-O3,-g的g++影响它的行为?
我正在玩Visual Studio上的单元测试.
试着测试一个简单的电话
Assert.AreEqual(2, 1, 2);
Run Code Online (Sandbox Code Playgroud)
运行测试时,它说通过?不应该失败.