久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Python 中模擬 os.listdir 來(lái)假裝文件和目錄

How to mock os.listdir to pretend files and directories in Python?(如何在 Python 中模擬 os.listdir 來(lái)假裝文件和目錄?)
本文介紹了如何在 Python 中模擬 os.listdir 來(lái)假裝文件和目錄?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)專有的存儲(chǔ)庫(kù)格式,我正在嘗試開(kāi)發(fā)一個(gè) Python 模塊來(lái)處理這些存儲(chǔ)庫(kù).回購(gòu)格式如下:

I have a proprietary repository format and I'm trying to develop a Python module to process these repositories. Repo format goes as:

/home/X/
       |
       + alpha/
       |
       + beta/
       |
       + project.conf

這里,X 是一個(gè)項(xiàng)目.alphabeta 是本項(xiàng)目中的文件夾,它們代表本項(xiàng)目中的.group 是這個(gè) repo 中的一個(gè)容器,它所代表的內(nèi)容實(shí)際上與這個(gè)問(wèn)題無(wú)關(guān).repo X 在其根級(jí)別也有文件;project.conf 就是此類文件的一個(gè)示例.

Here, X is a project. alpha and beta are folders inside this project and they represent groups in this project. A group is a container in this repo and what it represents is really not relevant for this question. The repo X also has files in its root level; project.conf is an example of such a file.

我有一個(gè)名為Project 的類,它抽象出X 等項(xiàng)目.Project 類有一個(gè) load() 方法,用于構(gòu)建內(nèi)存中的表示.

I have a class called Project that abstracts projects such as X. The Project class has a method load() that builds an in-memory representation.

class Project(object):

    def load(self):
        for entry in os.listdir(self.root):
            path = os.path.join(self.root, entry)
            if os.path.isdir(path):
                group = Group(path)
                self.groups.append(group)
                group.load()
            else:
                # process files ...

要通過(guò)模擬文件系統(tǒng)對(duì) load() 方法進(jìn)行單元測(cè)試,我有:

To unit test the load() method by mocking the file system, I have:

import unittest
from unittest import mock
import Project

class TestRepo(unittest.TestCase):

    def test_load_project(self):
        project = Project("X")

        with mock.patch('os.listdir') as mocked_listdir:
            mocked_listdir.return_value = ['alpha', 'beta', 'project.conf']
            project.load()
            self.assertEqual(len(project.groups), 2)

這確實(shí)模擬了 os.listdir 成功.但我無(wú)法欺騙 Python 將 mocked_listdir.return_value 視為由文件和目錄組成.

This does mock os.listdir successfully. But I can't trick Python to treat mocked_listdir.return_value as consisting of files and directories.

我如何模擬 os.listdiros.path.isdir在同一個(gè)測(cè)試中,這樣測(cè)試就會(huì)看到alphabeta 作為目錄,project.conf 作為文件?

How do I mock either os.listdir or os.path.isdir, in the same test, such that the test will see alpha and beta as directories and project.conf as a file?

推薦答案

我設(shè)法通過(guò)將一個(gè)可迭代對(duì)象傳遞給模擬的 isdirside_effect 屬性來(lái)實(shí)現(xiàn)所需的行為對(duì)象.

I managed to achieve the desired behavior by passing an iterable to the side_effect attribute of the mocked isdir object.

import unittest
from unittest import mock
import Project

class TestRepo(unittest.TestCase):

  def test_load_project(self):
      project = Project("X")

      with mock.patch('os.listdir') as mocked_listdir:
        with mock.patch('os.path.isdir') as mocked_isdir:
          mocked_listdir.return_value = ['alpha', 'beta', 'project.conf']
          mocked_isdir.side_effect = [True, True, False]
          project.load()
          self.assertEqual(len(project.groups), 2)

關(guān)鍵是 mocked_isdir.side_effect = [True, True, False] 行.可迭代對(duì)象中的布爾值應(yīng)與傳遞給 mocked_listdir.return_value 屬性的目錄和文件條目的順序相匹配.

The key is the mocked_isdir.side_effect = [True, True, False] line. The boolean values in the iterable should match the order of directory and file entries passed to the mocked_listdir.return_value attribute.

這篇關(guān)于如何在 Python 中模擬 os.listdir 來(lái)假裝文件和目錄?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 亚洲一区免费在线 | 久久精品欧美电影 | 精品日本中文字幕 | 成人一级片在线观看 | 亚洲精品一区二区三区蜜桃久 | 久久免费香蕉视频 | 天天噜天天干 | 日本久久久一区二区三区 | 伊人网伊人 | 欧美成人精品在线观看 | a久久| 国产激情在线观看视频 | 日韩精品一二三 | 国产亚洲欧美在线视频 | 国产精品久久久久久婷婷天堂 | 亚洲 欧美 日韩 精品 | 国产免费色 | 国产精品视频免费播放 | 夜夜爽99久久国产综合精品女不卡 | 91精品一区二区三区久久久久 | 最近最新中文字幕 | 99re6热在线精品视频播放 | 欧美一级淫片免费视频黄 | a级性视频 | 另类二区| 九九九视频在线观看 | 成年人精品视频在线观看 | 最新免费av网站 | 国产精品久久 | 亚洲综合婷婷 | 国内精品视频在线观看 | 人人做人人澡人人爽欧美 | 日韩精品视频中文字幕 | 久草在线| 日韩中文字幕在线播放 | 亚洲欧美一区二区三区在线 | 亚洲精品电影网在线观看 | 国产美女特级嫩嫩嫩bbb片 | 国产精品免费在线 | 欧美日韩在线视频观看 | 日本五月婷婷 |