简体   繁体   English

为 function 编写没有返回值的单元测试

[英]Write a Unit Test for function without return value

I have a function that takes a link as input, then requests values, then puts them in an available list, this function doesn't return anything, how can I write test cases for them?我有一个 function 将链接作为输入,然后请求值,然后将它们放入可用列表中,这个 function 不返回任何内容,我如何为它们编写测试用例? For example:例如:

list = []
def myfunc(link):
    [code block]
    list.append(value)

Generally it's not great to use global variables.一般用全局变量不太好。 I'd recommend using something like a class. eg:我建议使用类似 class 的号码。例如:

class Whatevs:
    def __init__(self):
        self.list = []

    def my_func(self,link):
        [code block]
        self.list.append(value)

Then in your test you can do something like:然后在您的测试中,您可以执行以下操作:

instance = Whatevs()
instance.my_func()
assert instance.list == [whatever it should be]

Also, I'd recommend reading PEP8.另外,我建议阅读 PEP8。 It just has some stuff about naming that you might find useful它只是包含一些您可能会觉得有用的关于命名的内容

Import the list, clear it, run the function and then check it's value:导入列表,清除它,运行 function 然后检查它的值:

from my_module import list, myfunc

def test_myfunc():
  list.clear()
  myfunc()
  assert list == [1, 2, 3]

I don't think it is very clean, but this would work: I made a new file to separate the production code from the testing code.我认为它不是很干净,但这会起作用:我创建了一个新文件来将生产代码与测试代码分开。 I imported the list (in my code it's l) and test on it.我导入了列表(在我的代码中是 l)并对其进行了测试。 My main.py:我的主.py:

from typing import Any

l = []

def myfunc(link: Any) -> None:
    my_actual_value = "my_expected_value"
    l.append(my_actual_value)

My main_test.py:我的 main_test.py:

import unittest

import main
from main import myfunc

class TestMyFunc(unittest.TestCase):
    def setUp(self) -> None:
        main.l = []

    def test_my_func(self) -> None:
        # Given
        some_link = "some_link"
        my_expected_value = ["my_expected_value"]
        # When
        myfunc(some_link)
        # Then
        assert my_expected_value == main.l

    def test_my_func2(self) -> None:
        # Given
        some_link = "some_link2"
        my_expected_value = ["my_expected_value"]
        # When
        myfunc(some_link2)
        # Then
        assert my_expected_value == main.l

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中,如何对没有返回值的函数进行单元测试? - In python, how to do unit test on a function without return value? 为了通过单元测试来测试 function,它是否需要有返回值? - In order to test a function with unit testing, does it need to have a return value? 如何在不打印返回值的情况下正确编写返回 function 方法? - how to write return function correct way without print return value? 使用 pytest.hookimpl 将 pytest 测试函数返回值写入文件 - write pytest test function return value to file with pytest.hookimpl 如何在 python 中为这个特定函数编写单元测试? - How to write unit test for this particular function in python? 如何为管理员保存功能编写单元测试 - How to write unit test for admin save function 如何在不更改其接口的情况下为发出网络请求的 function 编写单元测试? - how can i write unit test for function that is making network request without changing it's interface? 如何模拟具有特定参数的函数并返回在Python单元测试中调用另一个函数的值? - How to mock a function with specific parameter and return value that calls another function in python unit test? 如何对不返回任何内容的 function 进行单元测试? - How to unit test a function that does not return anything? 如何通过 python 中的单元测试为 init function 编写测试用例? - How to write test cases for a init function by Unit test in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM