带有文件io的python Unittest依赖关系

发布于 2025-02-04 05:32:07 字数 574 浏览 2 评论 0原文

我有两个函数

def complex_func():
   # generate result.csv
  
def post_process_func(csv_fname):
    # do something with result.csv

,我想为每个功能建立一个Unitest

def test_complex_func():
   complex_func()
   assert result.csv exists
   assert contents of result.csv is correct

,但是要测试POST_PROCESS_FUNC我需要result.csv,所以我需要调用confect> confects_func(),并且它是原因的。两个Unitests之间的依赖性问题。

我搜索并找到了模拟补丁,但是这些对于覆盖函数或对象的返回值而不是文件io很有用。

以Python的方式将这两种Undests分开的最佳方法是什么?

I have two functions

def complex_func():
   # generate result.csv
  
def post_process_func(csv_fname):
    # do something with result.csv

and I want to make a unittest for each function

def test_complex_func():
   complex_func()
   assert result.csv exists
   assert contents of result.csv is correct

However, to test post_process_func I need a result.csv so I need to call complex_func() and it causes dependency issue between two unittests.

I searched and found mock or patch but those are useful to override the return value of function or object, not the file IO.

What's the best way to separate these two unittests in python way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文