0

I have a function which returns a dataframe which has rows and columns. here's the function: app.py

def pandas_factory(colnames, rows):
    return pd.DataFrame(rows, columns=colnames)

And here is my unit testcase for the same: test_app.py

    def test_panda_fact(self):
        from dlp.dlp import pandas_factory
        df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
        with pytest.raises(TypeError) as er:
            mock_open = mock.mock_open(read_data=df1)
            with patch('dlp.dlp.pandas_factory', mock_open):
                obj = pandas_factory
            self.assertTrue(obj)

The testcase runs but am I testing it in the right way?

11
  • What is the point of the first test? You are not actually using pandas_factory() to create dataframes so what exactly is it you're trying to test here? Also, hard-coding test cases is generally not a great idea. Commented Oct 23, 2020 at 5:35
  • Can you explain what you're trying to do in the remaining test? Commented Oct 23, 2020 at 5:39
  • @pavel yes I figured that. please check the updated question Commented Oct 23, 2020 at 5:40
  • I created a new dataframe and im trying to patch it Commented Oct 23, 2020 at 5:42
  • 1
    I don't think it's a correct way of testing and I don't think you need a test here. What exactly do you think you need to test? The fact that pandas.DataFrame does what it is supposed to do? Or the fact that pandas_factory object exists? Commented Oct 23, 2020 at 7:07

1 Answer 1

1

So here's an example of how I would go about testing this function:

import pandas
import numpy as np

from dlp.dlp import pandas_factory

def test_type():
    df1 = pandas_factory(rows=np.array([[1, 2],[3, 4]]), colnames=['a', 'b'])
    assert isinstance(df1, pandas.core.frame.DataFrame)

def test_size():
    cols = np.random.randint(10, 100)
    length = np.random.randint(10, 100)
    data = np.zeros((length, cols))

    df = pandas_factory(rows=data, colnames=[str(i) for i in range(cols)])
    assert df.shape == (length, cols)

Something along those lines. Here I'm assuming that rows is some form of data, like a numpy array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.