question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: use cases (beyond which unit test lib I use)

See original GitHub issue

Hi,

@mrbean-bremen answered a question of mine in StackOverflow, and I got interested in this lib. I am a long time python user, but unfortunately I did not give the required attention to the importance of testing. I know basics (difference of fake, stub, mock, what is a unit of code, etc).

I am practicing testing with a new project that uses pytest, and it has a module that reads XLS reports using pandas and make consolidations on numbers. I think it has potential usage to test many/most methods there, but I am not really sure since documentation says: pyfakefs will not work with Python libraries (other than os and io), but I suspect pandas uses os and io under the hood.

I also would like to know if you recommend any good materials on unit testing with fakes/pyfakefs/pytest specifically.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
mrbean-bremencommented, Jun 7, 2020

@knightjdr , @staticdev: I just had another look at pandas, and at least for read_csv there is a possibility to use it with pyfakefs - just to let you know in case you are interested…

Pandas internally uses its TextFileReader to read from CSV, and this has an argument engine which defines which backend is used for file system access. The default is “c”, but if you set it to “python”, it will use the standard file system access and thus can be used with pyfakefs.

Patching TextFileReader accordingly will do the trick:

from unittest import mock

import pandas as pd
from pandas.io.parsers import TextFileReader

class PythonTextFileReader(TextFileReader):
    def __init__(self, *args, **kwargs):
        kwargs['engine'] = 'python'
        super().__init__(*args, **kwargs)

@mock.patch('pandas.io.parsers.TextFileReader', PythonTextFileReader)
def test_load_csv(fs):
    path = '/foo/bar.csv'
    fs.create_file(path, contents='1,2,3,4')
    df = pd.read_csv(path)
    assert (df.columns == ['1', '2', '3', '4']).all()

With pytest, this can be done using a separate fixture:


@pytest.fixture
def pd_fs(fs):
    with mock.patch('pandas.io.parsers.TextFileReader', PythonTextFileReader):
        yield fs

def test_load_csv(pd_fs):
    path = '/foo/bar.csv'
    fs.create_file(path, contents='1,2,3,4')
    df = pd.read_csv(path)
    assert (df.columns == ['1', '2', '3', '4']).all()

Unfortunately, this does not help for reading excel files - for that it uses the engine from one of several external packages. I will have a look at this later… would be nice to be able to do something here, as pandas is used a lot nowadays, and maybe integrate it into pyfakefs.

1reaction
staticdevcommented, Jun 16, 2020

@mrbean-bremen I will make tests ASAP. Unfortunately, too busy this week. It was added to my backlog =p

Read more comments on GitHub >

github_iconTop Results From Across the Web

use cases (beyond which unit test lib I use) · Issue #528 ...
I know basics (difference of fake, stub, mock, what is a unit of code, etc). I am practicing testing with a new project...
Read more >
Beyond Unit Tests. Most of us are familiar with the… - Medium
i.e., testing against a real server. It runs in the browser and works with any Javascript test framework such as Mocha, Karma, Chai,...
Read more >
A Deep Dive into Unit Testing - Semaphore CI
A unit is a class that can be tested in isolation from the whole system. Any external dependencies should be mocked. No stubs...
Read more >
Unit Tests, How to Write Testable Code, and Why It Matters
In this article, I will show that unit testing itself is quite easy; the real problems that complicate unit testing, and introduce expensive...
Read more >
Should anyone use unit test library methods and classes in ...
Yes, as you said, we often use non-unit testing libraries while writing unit test cases.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found