0

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? For example:

list = []
def myfunc(link):
    [code block]
    list.append(value)
11
  • 2
    Your test function needs to reset the global variable list to [], then call myfunc, then verify the contents of that global variable. BTW, I'd recommend using a different name for that variable, as list is a "reserved keyword" (which you are overriding when you use this variable name). Commented Feb 21, 2022 at 11:19
  • can i reset the variable list to [] in testing module bro ? Commented Feb 21, 2022 at 11:20
  • 2
    + very bad naming practice to name a list 'list' Commented Feb 21, 2022 at 11:20
  • i mean list [] then call function in file test.py ? Commented Feb 21, 2022 at 11:21
  • Sure. You'll just need to declare it global in the test function. Commented Feb 21, 2022 at 11:21

3 Answers 3

1

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. My main.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:

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
Sign up to request clarification or add additional context in comments.

1 Comment

i'm tried it in my test code and it helped, thank u so much
1

Generally it's not great to use global variables. I'd recommend using something like a class. eg:

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. It just has some stuff about naming that you might find useful

4 Comments

Hopefully he can change the code to better code... it's a better way.
yes, i dont want to change the code
@bbbbbbbbb it's easy to forget that there are people behind the text. Please remember that all the people who answer things here are actually trying to help folks and build a useful resource. In any case, best practice is to test the code you write yourself which implies some control over the code under test. When you dont test your own code then all sorts of weird things (like nasty global variables) slip into the code. Generally when code is written to make testing easy it makes a whole lot of other things easy as well.
I think that the main problem which this dude faces, is that he is required to test someone else's code, not under his control (which means that he cannot change it).
1

Import the list, clear it, run the function and then check it's value:

from my_module import list, myfunc

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

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.