我正在编写自己的JUnit Assert?我该如何测试?
我知道如何为它提供一些会通过的东西以及会使它失败的东西,但是如何为这些东西编写JUnit测试呢?
自定义断言将类似于:
public static void assertSomething() {
if (!something()) {
fail("Expected something, but ...");
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能抓到那个失败?
在方法中使用断言是不好的做法吗?
例如
def add(x, y):
assert isinstance(x, int) and isinstance(y, int)
return x + y
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
#include <cassert>
#include <string>
struct AStruct
{
int x;
char* y;
int z;
};
int main()
{
AStruct structu = {4, "Hello World"};
assert(structu.z == ???);
}
Run Code Online (Sandbox Code Playgroud)
我应该写什么来取代???成功的断言?
我用过assert(structu.z == 0);但很遗憾得到了错误
int main(): Assertion 'structu.z == 0 failed.Aborted'
我有相当多的代码,在开发版本中运行良好,assert()代码中有许多代码.我-DNDEBUG通过传递给g ++的指令禁用了断言,现在我的代码打破了seg.故障.有关assert()的东西我不知道吗?
有时候我会写代码
if (ptr)
ASSERT(ptr->member);
Run Code Online (Sandbox Code Playgroud)
代替
ASSERT(!ptr || ptr->member);
Run Code Online (Sandbox Code Playgroud)
因为它更直接的IMO.冗余比较是否会保留在发布版本中?
我正在尝试对我的python代码执行良好的输入有效性检查,但我也希望它简洁.也就是说,我不想要的解决方案就是这个:
def some_func(int_arg, str_arg, other_arg):
try:
int_arg = int(int_arg)
except TypeError, ValueError
logging.error("int_arg must respond to int()")
raise TypeError
try:
if str_arg is not None:
str_arg = str(str_arg)
except TypeError
logging.error("Okay, I'm pretty sure this isn't possible, bad example")
raise TypeError
if other_arg not in (VALUE1, VALUE2, VALUE3):
logging.error("other arg must be VALUE1, VALUE2, or VALUE3")
raise TypeError
Run Code Online (Sandbox Code Playgroud)
这只是太多的代码和很多空间,只需要检查3个参数.
我目前的做法是:
def some_func(int_arg, str_arg, other_arg):
try:
int_arg = int(int_arg) #int_arg must be an integer
str_arg is None or str_arg …Run Code Online (Sandbox Code Playgroud) 当断言失败并带有时间戳时,可以记录信息
恩.
int a = 10
assert( a > 100 );
Run Code Online (Sandbox Code Playgroud)
然后它将失败并输出就像时间戳一样
2013-12-02 , 17:00:05 assert failed !! (a > 100) line : 22
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在回答Java测试并遇到问题:
下列哪项为真?
A.在断言语句中,冒号(:)之后的表达式可以是任何Java表达式.
B.如果交换机块没有默认值,则认为添加断言默认值是合适的.
C.在断言语句中,如果冒号(:)之后的表达式没有值,则断言的错误消息将为空.
D.使用catch子句处理断言失败是合适的.
正确的答案是B.说实话,我通过排除另一个明显错误的案例回答了这个问题,但实际上我无法理解这个问题.谁能解释为什么这是真的?哪里有帮助?
我正在使用JsonUnit的assertJsonEquals
我在代码中执行以下操作:
assertJsonEquals(resource("ExpecedResponse.json"),
ActualResponse, when(IGNORING_ARRAY_ORDER));
Run Code Online (Sandbox Code Playgroud)
的ActualResponse具有从HTTP POST的响应。
该ExpectedResponse.json是一些字段的JSON文件如如下:
{
"columnNames": [
"date",
"signalType",
"userId",
],
"values": [
[
222555888,
"OUT",
"000-000-111-444"
],
[
333666999,
"IN",
"000-000-222-333"
],
],
"lastUpdatedTimestamp": "2018-01-26T00:00:00Z"
}
Run Code Online (Sandbox Code Playgroud)
我将这两个响应与assertJsonEquals进行比较。
我的问题是:如何告诉它忽略检查lastUpdatedTimestamp字段,而是使用assertJsonEquals或您推荐的任何其他库检查其他所有内容?
如果我从ExpectedResponse.json中删除了lastUpdatedTimestamp,那么它会抱怨它丢失了!
谢谢您的帮助,谢谢。
我创建了一个函数,它返回剩下的秒数直到下一次出现,但是我遇到了为它编写单元测试的问题.人们如何测试这种有调用功能的功能datetime.now()?
添加另一个参数(current_time)似乎是错误的,只是为了测试它,因为它改变了函数的初始要求.
功能测试是.
from datetime import datetime, time, timedelta
def get_time_left(target_time):
'''return float of number of seconds left until the target_time'''
if not isinstance( target_time, time ):
raise TypeError("target_time must be datetime.time")
curr_time = datetime.now()
target_datetime = datetime.combine( datetime.today(), target_time )
if curr_time > target_datetime:
target_datetime = curr_time + timedelta(1)
seconds_left = (curr_time - target_datetime).total_seconds()
return seconds_left
Run Code Online (Sandbox Code Playgroud)
对它的测试是.
class TestDTime(unittest.TestCase):
def test_time_left(self):
dt_now = datetime.now()
tm_5sec_future = ( dt_now + timedelta(0,5) ).time()
self.assertEqual( dtime.get_time_left(tm_5sec_future), 5.0)
Run Code Online (Sandbox Code Playgroud)
结果是. …