pytest 检查返回的列
pytest check columns returned
我在弄清楚如何 运行 时遇到了问题。我正在尝试创建一个测试数据框,我可以根据它来测试方法的输出。
@pytest.fixture
def keyword():
return ['bitcoin']
@pytest.fixture
def start():
return '2020-06-05T01'
@pytest.fixture
def end():
return '2020-06-05T04'
@pytest.fixture
def df():
df = pd.DataFrame(['2020-06-05 01:01:00', '1591318860',
'Jun 4, 2020 at 7:01 PM',
'7:01 PM', '[71]', '[True]', '[71]'],
index=['date'],
columns=['time', 'formattedTime', 'formattedAxisTime',
'value', 'hasData', 'formattedValue'])
return df
def test__get_cookies():
"""check if hardcoded values are True"""
pytrends = GoogleTrends()
assert pytrends.cat == 0
assert pytrends.gprop == ''
assert 'NID' in pytrends.cookies.keys()
def test__dict_request(keyword, start, end, df):
"""test the dict functionality"""
pytrends = GoogleTrends()
data = pytrends.interest_over_time(kw_list=keyword, start_date=start, end_date=end) # noqa E501
assert data.columns.equals(df)
这让我的 pytest 出错。我是 python 的新手,对 pytest 的经验更少。欢迎任何建议。
您遇到的问题是 pd.DataFrame
对象的初始化。
错误消息显示:Shape of passed values is (7, 1), indices imply (1, 6)
这表明您需要以不同方式传递数据。
我在您传递的负载中发现了两个问题:
'[71]'
好像重复了
- 您正在传递一个值列表,但您应该传递一个元组列表,其中每个元组的大小与列数完全相同。
不举例很难理解:
df = pd.DataFrame(
[
( # Notice the tuple here, inside the list
"2020-06-05 01:01:00",
"1591318860",
"Jun 4, 2020 at 7:01 PM",
"7:01 PM",
"[71]", # Notice this value appears only once
"[True]",
)
],
index=["date"],
columns=[
"time",
"formattedTime",
"formattedAxisTime",
"value",
"hasData",
"formattedValue",
],
)
(重新格式化以便更容易理解正在发生的事情)。
如果你像这样初始化数据框,错误就会消失,你会得到一个像这样的数据框:
time formattedTime formattedAxisTime value hasData formattedValue
date 2020-06-05 01:01:00 1591318860 Jun 4, 2020 at 7:01 PM 7:01 PM [71] [True]
但不确定它是否会修复您的测试:)
我在弄清楚如何 运行 时遇到了问题。我正在尝试创建一个测试数据框,我可以根据它来测试方法的输出。
@pytest.fixture
def keyword():
return ['bitcoin']
@pytest.fixture
def start():
return '2020-06-05T01'
@pytest.fixture
def end():
return '2020-06-05T04'
@pytest.fixture
def df():
df = pd.DataFrame(['2020-06-05 01:01:00', '1591318860',
'Jun 4, 2020 at 7:01 PM',
'7:01 PM', '[71]', '[True]', '[71]'],
index=['date'],
columns=['time', 'formattedTime', 'formattedAxisTime',
'value', 'hasData', 'formattedValue'])
return df
def test__get_cookies():
"""check if hardcoded values are True"""
pytrends = GoogleTrends()
assert pytrends.cat == 0
assert pytrends.gprop == ''
assert 'NID' in pytrends.cookies.keys()
def test__dict_request(keyword, start, end, df):
"""test the dict functionality"""
pytrends = GoogleTrends()
data = pytrends.interest_over_time(kw_list=keyword, start_date=start, end_date=end) # noqa E501
assert data.columns.equals(df)
这让我的 pytest 出错。我是 python 的新手,对 pytest 的经验更少。欢迎任何建议。
您遇到的问题是 pd.DataFrame
对象的初始化。
错误消息显示:Shape of passed values is (7, 1), indices imply (1, 6)
这表明您需要以不同方式传递数据。
我在您传递的负载中发现了两个问题:
'[71]'
好像重复了- 您正在传递一个值列表,但您应该传递一个元组列表,其中每个元组的大小与列数完全相同。
不举例很难理解:
df = pd.DataFrame(
[
( # Notice the tuple here, inside the list
"2020-06-05 01:01:00",
"1591318860",
"Jun 4, 2020 at 7:01 PM",
"7:01 PM",
"[71]", # Notice this value appears only once
"[True]",
)
],
index=["date"],
columns=[
"time",
"formattedTime",
"formattedAxisTime",
"value",
"hasData",
"formattedValue",
],
)
(重新格式化以便更容易理解正在发生的事情)。
如果你像这样初始化数据框,错误就会消失,你会得到一个像这样的数据框:
time formattedTime formattedAxisTime value hasData formattedValue
date 2020-06-05 01:01:00 1591318860 Jun 4, 2020 at 7:01 PM 7:01 PM [71] [True]
但不确定它是否会修复您的测试:)