在Python中获得一个功能

发布于 2025-02-10 21:06:53 字数 290 浏览 2 评论 0原文

我的功能可以从数据框架中获取列计数。

函数运行后,我想确保列计数与输入列计数相同。

因此,我在功能,colinput和coloutput中有两个变量。

当我在新文件中运行UNITSEST文件时,我正在插入函数(让我们称其为“ getWeight”(),

因此,从mainscript impert get

Weepaight中,我有两个列计数计算得出的两个列计数,

我如何编写一个比较colinput ==的测试的测试。 ?

coloutput

I have a function that gets the column count from a dataframe.

After the function is ran I want to ensure that the column count is the same as the input column count.

So, I have two variables inside the function, colinput and coloutput.

When I am running a unittest file in a new file, I am inporting the function (let's call it getweight()

so, from mainscript import getweight

Inside getweight I have the two column counts calculated

How can I write a test that compares colinput == coloutput?

I am getting stuck on the part of how to import those variables from the mainscript file.

Thanks in advance

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2025-02-17 21:06:53

将正在测试的计算分解为纯函数。确保MAIN可以导入而无需副作用(使用'__ -Main __'警卫):

main.py:

import pandas as pd

def getweight(x: pd.DataFrame) -> pd.DataFrame:
  # ...

if __name__ == '__main__':
  # run your computations

导入您感兴趣的主模块和测试属性:

unittest.py:

import pandas as pd
import main
import pytest

@pytest.mark.parametrize(
  'x',
  [
    # specify interesting inputs ...
  ]
)
def test_getweight_preserves_column_count(x: pd.DataFrame):
  assert len(x.columns) == len(main.getweight(x).columns)

*未测试

Factor out the computation under test into a pure function. Make sure main is importable without side effects (use '__main__' guards):

main.py:

import pandas as pd

def getweight(x: pd.DataFrame) -> pd.DataFrame:
  # ...

if __name__ == '__main__':
  # run your computations

Import main module and test properties you are interested in:

unittest.py:

import pandas as pd
import main
import pytest

@pytest.mark.parametrize(
  'x',
  [
    # specify interesting inputs ...
  ]
)
def test_getweight_preserves_column_count(x: pd.DataFrame):
  assert len(x.columns) == len(main.getweight(x).columns)

* not tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文