需要动态设置日期,以便测试不会失败

2024-09-28 19:02:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我编写的使用datetime.utcnow()的特定函数编写测试,这是必要的,因为它可以实时运行。测试的形状如下:

1.测试:

*Important_Date, in datetime format*

Function which uses Important_Date

*assertfunction*

2.使用Important_Date的函数

if Condition: return Important_Date < (datetime.utcnow() - timedelta(days=7))

现在,问题是,只要Important_Date距离datetime.utcnow()不到7天,测试用例就会通过。否则,它将失败

现在,要求是Important_Date应该设置为我从datetime.utcnow()得到的日期之前的1/2/3天。我不知道该怎么做

我试过datetime.utcnow() - 1,但不起作用

有办法做到这一点吗?感谢您的帮助

我使用的是PyCharm上的Python3,运行在Windows10上


Tags: 函数informatwhichdatetimedatereturnif