python - Hypothesis.strategies 从日期生成字符串
问题描述
我正在使用假设来测试我的应用程序并为端点生成随机输入数据。这是我的代码:
def generate_upload_data():
today = datetime.date.today()
start_date = today - relativedelta(months=1)
return hypothesis.strategies.builds(
SomeModelClass,
date=hypothesis.strategies.dates(
min_value=start_date, max_value=today
),
)
这会将日期生成为 datetime.date 对象,但我需要它以字符串格式(01.01.2020)。所以我需要像这样转换它
random_date.strftime("%d.%m.%Y")
但我找不到任何方法来做到这一点。是否可以从假设中的日期生成字符串?
解决方案
请参阅有关调整策略的文档。正如上面的 Azat Ibrakov 所说,您可以使用以下方法轻松地将日期转换为字符串
hypothesis.strategies.dates(...).map(lambda date: date.strftime("%d.%m.%Y"))
推荐阅读
- xcode - 如何在 Xcode 中恢复空的模拟器列表
- mysql - 在 mysql 表 TRUNCATE 之后需要 OPTIMIZE / ANALYZE 吗?
- parsing - 解析先前列表时的歧义
- react-native - 在测试 usinfg jest 时不存在反应本机内置全局对象
- exception - Spock 在 Where 块中测试异常处理
- python - gzip Python3无法将字节转换为字符串
- maven - 如何使用curl将jar作为maven工件发布到bintray?
- zipfoundation - ZipFoundation 存档可以作为邮件附件中的数据发送吗?
- button - 为什么在我的按钮样式中添加带有圆角半径的边框会消除视觉状态?
- android - RecyclerView 项目单击单击项目内的 TextView