#unit-testing

1 messages · Page 3 of 1

jaunty magnet
#

;compile

import time
import os

# os.system("cls")
inp = [
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬛⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬛⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬛⬛⬛⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
    "⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜",
]


def nextgen(inp):
    nxt = []
    alive = "⬛"
    dead = "⬜"
    for n, m in enumerate(inp):
        inp[n] = inp[n].center(len(inp[n]) + 2, dead)
    inp.insert(0, dead * len(inp[0]))
    inp.append(dead * len(inp[0]))
    for y, i in enumerate(inp):
        temp = ""
        for x, j in enumerate(i):
            count = 0
            tmp=[]
            if y == 0 or y == len(inp)-1 or x == 0 or x == len(inp[y])-1: continue
            for n in range(-1,2):
                for m in range(-1,2):
                    if n==0 and m==0:
                        continue
                    tmp.append(inp[y+n][x+m])
            count = tmp.count(alive)

            if inp[y][x] == alive:
                if 2 <= count <= 3:
                    temp += alive
                else:
                    temp += dead
            else:
                if count == 3:
                    temp += alive
                else:
                    temp += dead
            if y == 0 or y == len(inp)-1 or x == 0 or x == len(inp[y])-1: continue                   
        nxt.append(temp)
    nxt=nxt[1:-1]
    for n in nxt:
        print(n)
    print()
    return nxt


nxt = nextgen(inp)
while True:
    nxt=nextgen(inp)
maiden pawn
#

Oh my.

#

That is master piece i will save for interviews about refactoring

novel canopy
#

that's a lotta squares

maiden pawn
#

perfect for refactoring interviews too

#

the above ons is better though, for more hardcore. way more nestiness

novel canopy
#

lol

tacit dawn
#

hey guys i need help

#

can i post my code here

#

and ask for the problem ?

#

the code is about reversing the words on its place

#

hello my name to olleh ym eman

#

s=input("Enter the sentence")
l1= s.split()
z= len(l1)
l2=[]
a=0

for i in range(0,z):
k=l1[i]
l = len(k)
for a in range(-1,-l-1,-1):
h=k[a]

l2.append(h)

print(" ".join(l2))

vale vault
#

Building tests are somewhat kinda confusing I can't lie

maiden pawn
#

if test fails, it is always great instead of problem. u discovered a problem your test was written for 😁

hybrid ravine
#

I have some problem with Django.
when i run the command
python manage.py test

I am getting errors like this on the image

But when i delete init.py from application directory the error disappears
I will only add that the project works, the file structure is fine and all applications are added to settings.py

languid sentinel
#

How to approach testing code that is accessing azure infrastructure? I'm using Python SDK to access files on Storage Account

maiden pawn
#

Leaving turnable integration tests capable to check functionality of this class wrapper

#

in this way u will have tests running in majority without need for Azure access, and even covering functionality of this Azure interacting code too

weak knoll
#

Hi folks,
I am writing python for the past 6 years, but I never writen any tests
I would like to learn how to convert my code skills to be more TDD instead of YoLo 😄

Any good resources, advise where to start, etc ?

maiden pawn
maiden pawn
#

and then continue learning OOP/Design patterns/Code architecture. with books like Head First Design Patterns, Code Complete by McConnel, Clean Architecture by Robert Martin and etc, in order to get code architectural part of writing your code in order to have it testable

weak knoll
#

Thanks a lot

viscid basalt
#

Is it possible to provide a default value for fixture in pytest if it's not available? 🤔

# worker_id fixture might not be available here
def my_fixture(worker_id: str = ...) -> None:
   ...
dusty hamlet
#

Fununit 2.1.0 has released! Fununit is a functional, declarative unit-testing library for testing pure functions in Python.

  • functional: fununit has a functional API, which means it has great composability and flexibility
  • declarative: fununit lets you describe unit tests in a fully declarative manner
  • cuts out all code duplication: fununit removes up to 100% of unnecessary code duplication between tests

if you want to write unit tests that look like this:

multiply_tests = tests(
    ["Math"], "multiply", multiply, [
        ("zero", (0, 6), 0),
        ("identity", (1, 2), 2),
        ("positive", (3, 4), 12)
    ])

then check out fununit on github (https://github.com/johnmpost/fununit) or pypi!

GitHub

a functional, declarative, unit-testing library for testing pure functions in Python - GitHub - johnmpost/fununit: a functional, declarative, unit-testing library for testing pure functions in Python

marble wagon
#

I have some tests in a tests/ dir, and when I'm on linux I can run PYTHONPATH="$(pwd)" pytest so that my tests can access the modules in my root dir - What is the windows equivalent of PYTHONPATH="$(pwd)"?

maiden pawn
#

don't learn Windows. 😆

marble wagon
#

Yeah i dislike windows

placid heron
#

Is there a way to use TestCase / LiveServerTestCase server in Selenium to interact with the page?
Selenium runs the real server webpages now but i want to use the test env

royal orbit
ember maple
#

anyone aware of a library for making in-memory pathlib compatible paths
i want to get away from always writing to temporary folder when testing interaction with specific fs state

ember maple
proud nebula
#

ah

ember maple
#

i'd like to have soemething like a DictFs({"foo":{"test.md": "# content"}}) an the use it like pathlib

#

its a major blunder that pathlib has no concept of "filesystem"

proud nebula
#

That sounds like a pretty great lib idea :P

ember maple
#

well, no can do from me ^^ im stretched already ^^

proud nebula
#

Know that feeling. I'm writing a totally new i18n system for Django now.. for some damn reason heh

ember maple
#

oh thats damned

#

@proud nebula i18n apis are close up with date apis ^^

proud nebula
#

It's not actually that horrible heh

#

Maybe I overstated the case a bit. I'm still going to use the Django gettext/gettext_lazy functions. Just a much better string collection system, with no dependencies to unix gettext (and the ENORMOUS hacks Django has to enable that)

#

And just in general vastly improve the DX of the thing. Cut the number of gettext() calls in models to almost nothing by default for example.

proud nebula
#

hammett is very neglected though

ember maple
#

well, i wanted to start cogs of testing in 2020 to externalize gnarly part of pytest for reuse, its 2023 now an im about to go on parental leave with the 2nd kid instead

proud nebula
#

Hah, well there's no big idiotic project quite like a kid eh? Kinda puts the other things in perspective

ember maple
dusty hamlet
# royal orbit > Generate tests programatically for property-based testing. Are you thinking of...

I think it's doable. You're right though, it would have to be slightly different than the basic fununit notation. But I think you could just list the expected output value - you'd just be generating a bunch of separate unit tests with different parameters and expected outputs. I'd need to do some more brainstorming to see if this works for a variety of property-based testing scenarios. I might try to build this in as a feature if I come up with something

#

Hmm, it definitely depends on what kind of property you are testing... I'm gonna have to think more about this

viscid basalt
proud nebula
viscid basalt
viscid basalt
#

True, is pony orm just a personal preference? Never used it myself

proud nebula
viscid basalt
proud nebula
viscid basalt
uneven beacon
#

i could have sworn this was working a few weeks ago, but for some reason my pytest suite stopped being able to import the parent tests/ package: ```py
..\project> pytest tests\cogs\names
===== test session starts =====
platform win32 -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: ..\project, configfile: pyproject.toml
plugins: asyncio-0.20.3, benchmark-4.0.0, cov-4.0.0
asyncio: mode=Mode.STRICT
collected 24 items / 1 error

===== ERRORS =====
_____ ERROR collecting tests/cogs/names/test_cog.py _____
ImportError while importing test module '..\project\tests\cogs\names\test_cog.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\cogs\names\test_cog.py:7: in <module>
from ... import *
E ModuleNotFoundError: No module named 'tests'
===== short test summary info =====
ERROR tests/cogs/names/test_cog.py
!!!!! Interrupted: 1 error during !!!!!
===== 1 error in 0.16s =====file structure is setup as:py
project/
src/package/...
tests/
cogs/names/
test_cog.py
from ... import *
init.py # contains fixtures
pyproject.toml
... # poetry backend
[tool.pytest.ini_options]
addopts = "--import-mode=importlib"``` i also tried creating a fresh environment with virtualenv + poetry install and also downgrading pytest to my last used version, 7.2.0, to no avail

viscid basalt
#

@uneven beacon Why would you from ... import *? 🤔

uneven beacon
#

i have my common fixtures in the tests root

viscid basalt
#

You can use conftest 🤔

uneven beacon
#

hmm ill look into that, i just dont get why it was perfectly fine before until i took a break and it broke down

#

does conftest actually allow you to use variables defined inside it, or is it just to resolve the parameter names? my init.py exports some constants that id like to use for typehinting the fixtures correctly

viscid basalt
#

It allows you to use fixtures

uneven beacon
#

so not enough to cover my desires... 😔

viscid basalt
#

Maybe it does, what kind of variables do you need?

uneven beacon
#

just the fixture return annotations, in my case one type alias and one class from the source package

#

perhaps worth noting that python doesnt crash if i import the test files directly, e.g. py -m tests.cogs.names.test_cog

viscid basalt
#

I'd avoid using relative imports, unless module is on the same level, e.g. from . import some_module

#

You should put your fixtures in conftest.py, so they're automatically discovered by pytest

proud nebula
uneven beacon
#

apparently moving my fixtures into conftest.py while retaining a single __init__.py in the tests/ root fixed it, but for convenience im still importing conftest directly so i can still have my fixtures' typehints

viscid basalt
#

Honestly mistyping fixtures in your test might be an issue that's currently not solved, unless there's a flake8/mypy/pytest plugin for that:

#
@pytest.fixture
def some_fixture() -> int:
    return 42

def test_something(some_fixture: str) -> None:
    ...
uneven beacon
#

inferring it would be ideal but eh :shrug:

viscid basalt
#

Well, it's like 5 lines and could be ported to any codebase

uneven beacon
#

it seems like the plugin wouldnt cover much that your type checker cant do

  1. if you correctly typehinted your fixture but misused it, either your type checker complains or your test raises a runtime error
  2. if you gave a generic typehint like Any or didnt typehint the fixture, typeguard wouldnt complain and you'd get a runtime error if the fixture was misused
  3. if you mis-typehinted your fixture, either your type checker complains first, typeguard raises a runtime error slightly earlier than your test would have, or it catches a typehint bug that doesnt impact your test
viscid basalt
#

Typecheckers are not aware that fixtures are somehow related to your test parameters

#

Also your fixtures may come from different modules and/or plugins, I would be too lazy to import them

proud nebula
viscid basalt
#

Which is "great"

#

I think it's a part of pycharm integration with pytest, but I don't think mypy/pyright support that, at least not mypy 🤔

proud nebula
viscid basalt
#

It removes annotations from tests where that fixture is used:

@pytest.fixture
def number() -> int:
    return 42

def test_number(
    number: int,  # type hint would be removed
) -> None:
    pass
proud nebula
#

I don't understand how you can stand having that much type annotations everywhere though heh. Might as well write Rust.

proud nebula
#

Well.. there are really a ton of issues it's been years for :/

#

They broke del unused a few years ago for example. Quite annoying since it used to work.

#

That's my biggest beef with pycharm honestly, that they don't seem to have regression tests for stuff.

lament heath
lavish vault
#

Is it pretty common to do test driven development. I cant seem to get in the rythm of it and habits of not adding tests just keeps lurking

maiden pawn
maiden pawn
twin tusk
thorn bluff
twin tusk
#

For me, the major advantage of TDD is, even if you don't follow it fully and don't write tests first, it puts the question "how would I test this?" permanently in your mind as you code.

maiden pawn
#

You can skip TDD, but TDD will not skip you

final nest
#

anyone with experience using locust?

proud nebula
sleek sluice
#

Hello!

im new to unit-teting.

I am looking to test a Flask-Rest-API. What would be a good practice to test out all the endpoints and class methods;

Would it be better to run a mock-up version of the server, and test out the endpoints using various HTTP requests?

or should i focus on testing out the return values of each function/method of each endpoint?

ember maple
# sleek sluice Hello! im new to unit-teting. I am looking to test a Flask-Rest-API. What woul...

strongly depends on the intended logic behind it - i'd recommend to move tricky stuff into function that you can call directly for a test , and then having a few select test that ensure everything works together

for the tricky bits one really wants the surrounding shenanigans out of the way, for the rest ensuring all things work together is a bit important, also make use of type annotations to have your back

sleek sluice
#

If i may add another question; is there a convention on how to deal with objects that are initiated and imported from other modules?

Like a logging instance i imported into the specific module i want to test.

#

It would throw an error, because the defined relative directory does not exist from the current called module

ember maple
maiden pawn
sleek sluice
# maiden pawn Python unittest in Mock patch can mock even global stuff if desired But it is a ...

I think my issue is that i cant seem to override modules that are irrelevant for the specific unittesting but are necessary for general Runtime of the application.

here is a clarification in code:

the file/module i want to test a function in

import logging, re
from logging.handlers import TimedRotatingFileHandler


def get_logger():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    handler = TimedRotatingFileHandler("./logs/historie.log", when="midnight", interval=1)
    logger.addHandler(handler)
    
    return logger


log = get_logger()


def return_one():
    return 1

and the unittest file:

import unittest
from unittest.mock import patch


class TestExt(unittest.TestCase):

    @patch("ext.get_logger")
    def test_return_one(self):
        import ext
        self.assertEqual(ext.return_one(), 1)


if __name__ == "__main__":
    unittest.main()

this unittest still throws an error because it is still calling the get_logger() function.

Basically i want the ext.py module to be imported but the setup of the logging object to be ignored. And patch didnt help in this case

EDIT:
i just learned through research that the code inside of modules have to be run anyways. And there is only the option to avoid running code in a module is by mocking the whole module, but it will give no access to the objects/classes/variables... etc in the module.

It seems that i'll have to implement the if __name__ == "__main__": conditional in most of my modules to avoid such errors.

dense bough
#

My understand of how this works:

  • patch assigns to ext.get_logger, making it available to the test;
  • import ext rebinds ext;
  • At this point, if get_logger is not defined in the module, then ext.get_logger is undefined.
maiden pawn
hollow basin
#

@thick star have you tried using pytest with pytest-mock?

#

your code

def start_time():
    current_time = datetime.now()
    return current_time
    
class TestStartTime(unittest.TestCase):
    def test_start_time(self):

        expected_time = datetime.datetime(2023, 2, 8, 10, 0, 0)
        datetime.now = MagicMock(return_value=expected_time)

        result = start_time()

        self.assertEqual(result, expected_time)
#

with pytest and pytest-mock

def test_start_time(mocker):
    mocker.patch("datetime.datetime.now", return_value=datetime.datetime(2023, 2, 8, 10, 0, 0))
    expected_time = datetime.datetime(2023, 2, 8, 10, 0, 0)
    result = start_time()
    assert result == expected_time
#

pytest is better than unittest in pretty much every way. a lot of people treat it as the default unit testing framework, but it's easier to develop it separately from the language itself.

proud nebula
hollow basin
#

idk what that is 😛

maiden pawn
#

pithink i remember briefly it before

#

frozen time or something like saw somewhere

#

freezegun

#
@pytest.mark.freeze_time('2019-10-04')
maiden pawn
#
from freezegun import freeze_time

freezer = freeze_time("2012-01-14 12:00:01")
freezer.start()
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0, 1)
freezer.stop()

controlling flow of time 😆

#
@freeze_time("Jan 14th, 2020", auto_tick_seconds=15)

even speed

proud nebula
#

It's pretty much dead, has lots of bugs, and the basic approach isn't really good

#

(I'm funnily enough one of the maintainers.. but I've given up)

dawn frost
#

Have any of you tried JetBrains Aqua? I keep seeing it, but don’t really understand how it’s different from normal IDEs yet

dawn frost
#

ig not 😦

weary raptor
thick star
#

@hollow basin Very belated thanks for you reply. Sadly though, pytest complains in oh so many words about the same problem: TypeError: can't set attributes of built-in/extension type 'datetime.datetime'

plucky ice
#

Hey is this for selenium? Channel

proud nebula
dawn matrix
#

Hey, I am testing a Class where I read some json data from a file. When running the test, the path for the files is wrong (because my tests are one stage lower than the class). How can I solve this problem. I am using pycharm.

proud nebula
#

Maybe show a small code example?

proper wind
#

hey guys

#

im making code for linux on windows

#

how would i test it?

proud nebula
plucky ice
#

I am trying to do selenium set speed 10

#

But it isn’t working in python

#

I don’t get it been trying for hours

#

Keeps saying invalid syntax

plucky ice
#

I am just trying to slow it down because it’s so fast

maiden pawn
#

answer is: run tests in Github Actions / Gitlab CI 🙂 they will rerun quickly for LInux. Or develop already from linux / or work from case sensetive mounted filesystem

maiden pawn
proud nebula
proud nebula
dawn matrix
# maiden pawn <@690300553653256265> i can guess he means, path files are working incorrectly a...

Sorry, I badly expressed. I won´t deploy to Gitlab or working with linux. This is a project for my University ( so developing on windows :D) . I meant that I have the following folder structure: poker(folder) here is my python script and my json files. In the poker folder I have a nother folder for tests. And here are my scripts. Now if i run the scripts, the paths for the json files are not found. The paths are (./json_file_1.json). I need to change the paths to (../json_file_1.json). Now the tests would work, but if I only run the script, it wont find the paths.

mellow thicket
proud nebula
maiden pawn
maiden pawn
#

that will make it searched strictly relative to the file from where you have written this script

#

tests will not be able to change such path

#
  1. also in a same way you can discover project folder once for all your scripts pathlib.Path(__file__)
    and having other scripts using this once discovered path to build based on it their paths
#

from project.settings import project_folder
path = str(project_folder / "folder" / "file.json"

cobalt ore
#

is there a way to test conftest.py ? I can run a particular test with pytest -k '<filter>', but was wondering if there was a way to just run the conftest with pytest 🤔

note - i dont' mean write a separate test for conftest, just to run pytest without actually running any tests (which to me means it'd run the conftest to setup and not actually do anything). But i might be mixing some things up

proud nebula
cobalt ore
craggy jolt
#

where do i use coverage run -m unittest discover

#

and what is the general process for test harnesses in python?

#

what does that mean in the context of python

#

is it just unittest?

proud nebula
rotund dagger
#

I have an object which is either a string or a pytest.mark.structures.ParameterSet. How can I import ParameterSet so I can do an isinstance(obj, ParameterSet) ?

#
Variables:
m: MarkDecorator [pub]
| .args: tuple (2)
| .kwargs: dict (0)
| .mark: Mark [pub]
| | .args: tuple (2) [pub]
| | | 0: 'meow'
| | | 1: list (1) [pub]
| | | | 0: 'kitty' <---------------
| | .kwargs: dict (0)
| | .name: 'parametrize'
| .markname: 'parametrize'
| .name: 'parametrize'
m2: MarkDecorator [pub]
| .args: tuple (2) [pub]
| | 0: 'meow'
| | 1: list (1)
| .kwargs: dict (0)
| .mark: Mark [pub]
| | .args: tuple (2) [pub]
| | | 0: 'meow'
| | | 1: list (1) [pub]
| | | | 0: ParameterSet [pub] <-----
| | | | | 0: tuple (1) [pub]
| | | | | | 0: list (1) [pub]
| | | | | | | 0: 'kitten'
| | | | | 1: tuple (0)
| | | | | 2: 'P'
| | .kwargs: dict (0)
#

from

#!/usr/bin/env python3

import pytest

m = pytest.mark.parametrize("meow", ['kitty'])
print(m)

m2= pytest.mark.parametrize("meow", [
    pytest.param(['kitten'], id = 'P')
])
print(m2)
#

print(type(m2.mark.args[1][0])) results in <class '_pytest.mark.structures.ParameterSet'>

#

however, isinstance(m2.mark.args[1][0], pytest.mark.structures.ParameterSet) results in

AttributeError: 'MarkDecorator' object has no attribute 'ParameterSet'
hearty oriole
#

Hello

#

Anyone who is good at mocking a database insertion for a unit test? Because I'm having difficulties with mocking one 😅

proud nebula
fiery arrow
#

Suppose I'm writing API tests for a parental control service. I might want to create a "family" like this:

sample_family = {
  "id": "family-smiths",
  "name": "Smiths",
  "parents": [
    {
      "id": "dad-smith", ...
    },
    {
      "id": "mom-smith",
      "authorize_devices": [
        {
          "id": "mom-smith-iphone", ...
        },
      ],
    },
  ],
  "children": [
    {
      "id": "child-alice-smith", ...
    },
    ...
  ],
}

The problem being, of course, that I can't hardcode the IDs, they're generated randomly. Is there a neat way of defining such things so that I'm able to e.g. pass them to pytest.mark.parameterize?

#

Like, in the test data I might include some more information on how to create the family, then the parents, then the children etc. -- that's where I'd get the IDs, step by step. And in the end I want to check that the result looks like the above^ (more complicated in reality of course)

maiden pawn
# fiery arrow Suppose I'm writing API tests for a parental control service. I might want to cr...

i would advice inspiring with code approach of factoryboy (each sub objects is created automatically with sub factory. all attributes are created with fake data, you are having precision to create fake objects only in relevant parameters you need. It can help to minimize data.
https://factoryboy.readthedocs.io/en/stable/introduction.html

And you are certainly can be using pydantic https://docs.pydantic.dev/ library as main source of your... reusable body requests.
you can define your objects once as its BaseModel, and with obj.json() or obj.dict(), it will make nested serialization to your end desired view
Together with utilizing Pydantic Field(default_factory=list | lambda: write your own generator) you can essentially create all your request bodies pithink

plucky ice
#

Hey is there software testing channel for appium

#

?

fiery arrow
#

I don't want to create random data in the test. The IDs will be generated on the server side

ember maple
rotund dagger
spring nebula
#

anyone have any ideas on how to make pyautogui not go for the same image twice when both images are the same images

ember maple
jolly tangle
#

Hello super quick question I am writing code in pytesting and in the tutorial they have 2 folders unit and functional. What goes in each folder and what is the definition of each? I am using a flask web application.

proud nebula
jolly tangle
#

@proud nebula By "end to end" I assume you mean like testing routes. By "functional tests" I assume you mean functions. Is this correct?
Thanks

proud nebula
#

Like I said, the names are quite bad.

#

End-to-end usually means testing the entire stack, by running a web browser to test functionality. You want as little as possible of those because they are super slow.

viscid basalt
earnest sequoia
#

test_query_<query_name>?

viscid basalt
proud nebula
#

using pytest obviously

viscid basalt
#

Is it a unit test? Integration test?

proud nebula
#

You have to have a pretty big test suite before you split these up and care much about the names though imo.

viscid basalt
#

I could always use marker if I need to, currently my tests directory structure is the same as my main code

viscid basalt
earnest sequoia
#

if you are only testing a specific query, I would say it is a unit test of the query itself

#

if it involves multiple queries or a whole transaction then I think it would be an integrated test

viscid basalt
earnest sequoia
#

yeah I'm doing the same thing, the ORM makes it harder to separate database from the business logic

#

however

#

using data transfer objects between modules can greatly reduce the need for creating database objects as the data transfer objects themselves can be easily used as stubs

viscid basalt
viscid basalt
#

Unless it can't be represented as existing model, then I make a dto for a result of that specific action

vivid cosmos
#

is testing in flask necessary

viscid basalt
vivid cosmos
#

like I just dont see any point of me testing in flask because of the nature of it

#

im doing it as a side project to get a internship

viscid basalt
#

@ember maple Is _pytest.* considered a public api? 🤔

viscid basalt
ember maple
viscid basalt
#
@pytest.fixture(scope="session", params=["asyncio", "trio"])
def anyio_backend(request: SubRequest) -> str:
    return request.param

How would I annotate request then? 🤔

#

I also use this hook/plugin to check if test parameters are annotated properly:

def pytest_runtest_makereport(item: Function, call: CallInfo[Any]) -> None:
    if call.when != "call":
        return

    for fixture_name, type_ in item.obj.__annotations__.items():
        if fixture_name not in item.funcargs:
            continue
        typeguard.check_type(fixture_name, item.funcargs[fixture_name], type_)

Function and CallInfo are a part of private API too

ember maple
#

both function and callinfo are exported in pytest already

viscid basalt
ember maple
#

seems like subrequest is not the way to go atm

#

hmm, oh, thats a issue that may need collaboration from pycharm,

viscid basalt
#

There's also another very annoying bug, related to pytest - renaming any fixture erases type annotations from tests where its used

ember maple
#

ok, sub-request is not exposed for good reasons it seems (aka even fi you used it, the types woudl be wrong

viscid basalt
#

I see, by the way - is there a plan to add an api similar to pytest.mark.parametrize but that could be used programmatically? 🤔

#

Similar to pytest.fixture(params=...)

ember maple
#

pytest_generate_tests ?

#

the parameterize marker is implemented by that hook, fixture params too

viscid basalt
#

I see, I should probably take a look 👀
Some of my parametrized data also depends on other fixtures/configuration, so having a function return a list of parameters would be better

ember maple
#

parametrize cant use fixtures

#

it happens before setupstate is made, setupstate is where fixtures get set up and torn down

viscid basalt
#

I know, that's why I'm saying that I need something different

viscid basalt
vivid cosmos
viscid basalt
#

You can test your internals, or responses from your flask app, If you're using templates then testing responses is probably not worth it

#

But you still can test internals of your application

ember maple
#

@viscid basalt a plugin class that uses pytest_configure/unconfigure + provides the additional hooks may work, then you contain the state in your plugin object

vivid cosmos
#

so i just do a brief tests

#

?

viscid basalt
vivid cosmos
#

brief overview

#

not like very very comprehensive

viscid basalt
#

Are you making an API or does it use templates?

vivid cosmos
#

templates

viscid basalt
#

You can test that your templates at least render properly
Also it shoud be easier to test that your templates actually receive correct data

#

If you move logic away from flask endpoints you should be able to test it separately

#

But if your site is not dynamic (e.g. it doesn't use any kind of storage/database) then there's not much to test 🤔

viscid basalt
maiden pawn
# vivid cosmos is testing in flask necessary

is good quality of code necessary for smth?
not really for simple very small stuff that will be thrown away and never improved for new features, or ever taken care by other developers. Otherwise better to take more care

ember maple
#

hmm, testing tends to feel unnecessary until the lack of tests bites you in the back cause our house was made of cards instead of something more solid and you never knew ^^

#

conversely adding tests typically brings lots of little hidden human errors to the light, just try to break it a little, and oh boy it will - in particular in the face of change

maiden pawn
#

And once u get hooked to typing, u can't live already without it

ember maple
#

yeah, refactoring is way better without the blind duck

jolly tangle
#

@proud nebula thanks

gloomy crow
#

When running a bunch of tests with pytest, is there any way to automatically continiue on failed ones?

earnest sequoia
#

Doesn't it already do that by default? You have to pass -x flag for pytest to terminate on the first failing test

gloomy crow
#

Hmmm, i guess it could be --pdb flag then

ember maple
gloomy crow
#

What happens is that it enters debug in each failed test, so i have to type c

#

Which makes me unable to trully go afk while i run all tests(3000+) 😄

hearty oriole
proud nebula
proud nebula
hearty oriole
brazen needle
weary quarry
#

At some point you will have a function who has the dutiful job to handle a crud operation to the database. That function's behavior should also be tested. Depending on the setup I've simply replaced the database connection with an in-memory sqlite3 connection or have a database spun up in a docker to the side. I "mock" the database by replacing the production database with something testable.

#

Both options I've used are made simpler by having said database connection be an injected dependency to the function/class that uses it. That makes replacing that connection much easier in the test. The function doesn't care about what connection it runs the crud on, only that it has one.

#

And at that point you could just replace the connection with a MagicMock() and assert for calls, but it's almost as easy to spin up a testing database. You have the bonus of testing behavior and function of your crud operations with the latter. :)

#

"Just don't mock" is a neat idea. A lot like "don't use if statements". It works, until it doesn't.

brazen needle
#

I like the idea of using an in-memory sqlite3 database. I feel like mocking tends to be fragile; it's hard to get the mock's behavior right. But in a way, database interaction is something that you can't actually "unit test" in the strict sense because it's fundamentally about an interaction between your code and someone else's.

weary quarry
#

I agree. The unit test should be testing the behavior of the function. If I give you X and Y I assert you perform Z. I just see it as an added bonus that you can also assert your queries are doing what they should in the case of the database.

Change the third party target to, say, an API and I change my tune. Mock the call, assert the behavior of your code, and don't try to test the API. :)

proud nebula
#

that's not a great function, you want at least SOME sleep() in there!

viscid basalt
#

I think you could wait for N seconds and raise a specific Timeout error if it didn't complete 🤔

#

This can be done using threads or signals on linux

#

Or tasks if you're using asyncio 🤔

#

I think it would also be a good idea to add timeout prameter to wait_until_not_busy

ember maple
#

timeouts are needed, threading primitives should be used ^^

#

if you arent allowed to change the function, then i hope its part of a bad exercise and not something in production

jolly tangle
#

I am running some pytesting code with the command in the terminal by typing python - m pytest. How do I print, print("something") from conftest.py fixtures to the terminal in Visual Studio Code? My setup is similar to this https://gitlab.com/patkennedy79/flask_user_management_example . Here is the link where I got the uploaded code https://testdriven.io/blog/flask-pytest/ . Though my code is a decent amount different.

Side note every folder has a __init__.py file. I just didn't include it in the diagram below.

testing folder 
|-----conftest.py
|
|                                         
|                           
|------------------------------ functional   folder    |
|                                                      | 
|                                                      | -----------------------------------test_routes.py
|                                           
|                                           
|---------------------------------------- unit folder  |-----------------------------------test_functional.py
|                                                      |-----------------------------------test_models.py

Thanks

craggy jolt
#

whats the structure of professional testing in python usually? is it just a file with different functions that test their respective functions?

weary quarry
#

Sure, usually. I don't know about the whole "professional" part of it. Given main.py I'll have a test/main_test.py and in that test file I'll have as many tests as needed to cover each unit and any branches.

#

I prefer main_test.py over test_main.py simply because it makes autocomplete easier.

rare atlas
#

having main_test.py also is nicer for sorting filenames if they're in the same directory

topaz socket
lethal river
weak drum
#

Also, what's f'/sentiment?url={url}' do you have a local sever running for /sentiment? if not, that's not a valid url

jolly tangle
topaz socket
#

I got same problem and simply searched the web...

jolly tangle
#

@topaz socket how did u phrase the search?

weary quarry
#

"display print output pytest" brings up several articles. The pytest documentation has the cli option as well. -s is the equivaent to --capture=no.

vapid field
#

idk if this the right place to ask but i'm new in coding and i wrote a simple code to choose a random letters and i want the output to be in insta like to check if the username is valid or not but idk how to connect them anyone can help?

daring coral
#

Hello all. I'm struggling wrapping my head around pytest's fixtures and the class scope. I'm testing a client/server project. While testing the client I need to spin up an instance of a mock server, this is done by calling an exe. I want the mock server to persist for all of the tests in a given class and terminate after all client tests are run. Would this be done with pytest.mark.usefixture before the class definition or do I place the mock server fixture in each of the test methods inside the class. thanks in advance for any help.

maiden pawn
#

someone named themselves pytest :\

daring coral
#

Thanks for the reply. I will give that a try in the morning.

sharp sparrow
#

guys, how can i monkeypatch a function in my own module for pytest?
code structure

src
 - module-name
    - utils.py
      - func1(): ...
tests
  - conftest.py
    - replacement_func(): ...
    - conftest_func_name(): ...
  - test_demo.py
    - test_demo1(conftest_func_name):

so, basically, i need to monkeypatch the func1() function from my module with replacement_func() function in the conftest.py file
and i have no idea how to do it
any ideas?

proud nebula
ember maple
sharp sparrow
split field
sharp sparrow
#

and its not working

split field
#

if the callers of func1 do from module_name.utils import func1, then monkeypatching module_name.utils.func1 after they've done that isn't enough - you need to also monkeypatch the_module_that_already_imported_utils.func1

sharp sparrow
split field
#
monkeypatch.setattr(module_that_did_the_import, 'func1', replacement_func)
#

(that's written for unittest.mock.patch, but everything it says about where to patch applies equally to the monkeypatch fixture)

sharp sparrow
#

im confused

split field
#

if the user does from yourmodule import func1, then they have a reference to func1. If, after that point, you change yourmodule.func1 to be a different object, that doesn't affect the reference that user has to the original func1

sharp sparrow
split field
#

it doesn't matter why you need to patch it. What matters is that you need to replace every reference to it (or at least, all of the references to it that might be called by the test)

sharp sparrow
#

@split field then do i need to change it manually in the test function like this? (what u r telling is too much complex for me)

import module_name

def replacement_func(): ...

def test_demo1():
  module_name.utils.func1 = replacement_func

  # do stuff

 # and i dont know what to do to  get it reverted back to its original function 
split field
#

I told you to call monkeypatch, and what you're showing doesn't call monkeypatch. I don't know how you got to that from what I said.

#

!e ```py
import math
from math import pi

print(f"{math.pi=}")
print(f"{pi=}")

Now monkeypatch math.pi to a new value!

math.pi = 3

print(f"{math.pi=}")
print(f"{pi=}")

bitter wadiBOT
#

@split field :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | math.pi=3.141592653589793
002 | pi=3.141592653589793
003 | math.pi=3
004 | pi=3.141592653589793
split field
#

if you've done from math import pi, then later monkeypatching math.pi doesn't affect the already-imported pi

sharp sparrow
#

and as far as i've seen, i need to change every reference of the func1 right?

#

and also, im not directly using that function by the way
I'm actually calling it by calling another function inside the same module (sorry for telling this late)

# module_name.utils
def func1(): ...
def func2():
  func1()

# test_demo.py
def test_demo1.py
  func2()

# conftest.py
import module_name

# inside a fixture function 
monkeypatch.setattr(module_name.utils, 'func1', replacement_func)
#

i hope that i've given enough details to understand this thing even more clearly

split field
#

can you share your actual code?

#

your simplifications aren't helping people understand, because I think you're dropping relevant information.

sharp sparrow
# split field can you share your actual code?

I'm not having my laptop right now, so let me just tell you even more

the func1 has an infinite while loop in it like this

module_name.utils
def get_command_func():
  return sys.stdin.readline

def func1():
  get_command = get_command_func()
  while True:
    command = get_command()
    if command == 'exit':
      return
    # doing some command execution

def func2():
  # some code in here
  func1()

and all i need to do is to mock the user input for the tests

for that, i am trying to do something like this

# conftest.py
import module_name, pytest

a = iter(('help', 'exit'))

def replacement_func():
  yield next(a)

@pytest.fixture
def sample_input(monkeypatch):
  monkeypatch.setattr(
    module_name.utils, 'func1', replacement_func
  )

so, is there any way that makes this code work with some sample inputs?

split field
#

so every call to func1 is made by func2, and func1 and func2 are in the same file?

sharp sparrow
#

i mean, yes, every call to func1 is made by func2, and func1 and func2 are in the same file. and i'm using func1 by calling func2

split field
#

then absolutely none of the advice that people have been giving you about where to patch was actually relevant, and we've all been misunderstanding what your problem is

sharp sparrow
#

so what's the actual problem then? mocking stdin?

split field
#

I don't know - what's the problem?

#

what you're doing looks like it could work to me.

#

I'd rewrite ```py
a = iter(('help', 'exit'))

def replacement_func():
yield next(a)
aspy
def replacement_func():
yield from ("help", "exit")

#

but other than that, what you're describing looks OK to me.

sharp sparrow
#

it just looks ok, but that is not working at all
that's the thing

split field
#

does your test use the sample_input fixture?

sharp sparrow
split field
#

then I'm sure I won't be able to help more without actually seeing the real code.

sharp sparrow
#

the test function signature is this -

# test_demo.py
def test_demo1(sample_input, capfd):
split field
#

it sounds like you're patching the right place, the patch that you're describing should work, so if it isn't working, the reason why is likely something that's in your real code that isn't in your simplified examples.

split field
sharp sparrow
#

but i can post it here about 12 hours later (im in clg)

split field
#

ok. Without seeing it, the best I can say is that what you're describing sounds like it ought to work. If the only caller of func1 is in the same module as func1, and you're patching func1 in that module, then every call to it should call the thing you've patched in, instead.

sharp sparrow
#

@split field ok, thankfully i've the code in github

this is the func1 that im talking about - https://github.com/GhostOps77/click-repl/blob/GhostOps77-patch-1/src/click_repl/utils.py#L344

and this is the func2 - https://github.com/GhostOps77/click-repl/blob/GhostOps77-patch-1/src/click_repl/utils.py#L353

and the test function would be something like this (i do need to capture stdout, so im using capfd)

from click_repl import repl
import pytest

def test_demo(sample_input, capfd):
  # here, i have some basic click commands implemented here, something like this - https://github.com/GhostOps77/click-repl/blob/GhostOps77-patch-1/tests/test_repl.py#L7-L24

and i haven't pushed the conftest.py code, but this is the exact code that i've implemented

import click_repl, pytest

a = iter(('help', 'exit'))

def replacement_func():
  yield next(a)

@pytest.fixture
def sample_input(monkeypatch):
  monkeypatch.setattr(
    click_repl.utils, '_get_command_func', replacement_func
  )
GitHub

Subcommand REPL for click apps. Contribute to GhostOps77/click-repl development by creating an account on GitHub.

split field
bitter wadiBOT
#

src/click_repl/utils.py line 392

get_command = _get_command_func(isatty, session)```
sharp sparrow
#

so maybe, this would work

def replacement_func(a, b);
  # same thing

and now tell me, what should make the mocking stdin work?

split field
#

you can patch your own object in as sys.stdin if you want to - like an io.TextIO object

sharp sparrow
sharp sparrow
split field
split field
#
def set_stdin(monkeypatch, user_input):
    monkeypatch(sys, "stdin", io.TextIO(user_input))

@pytest.mark.parametrize("user_input", ["help\exit\n", "foo\nbar\nbaz\n"])
def test_something(user_input, monkeypatch, capfd):
    set_stdin(monkeypatch, user_input)
    repl(...)
sharp sparrow
split field
#

I think it would be better to put it in a util.py file in the same directory as conftest.py, and do from .util import set_stdin wherever you need it

#

in theory you can import stuff from conftest.py too, but that's not what it's for, and I'd be worried about stuff running twice that shouldn't

sharp sparrow
split field
#

"the user" of what?

#

it should be in your tests directory, and that whole directory is for maintainers, not for users

sharp sparrow
#

it should be in the test directory
i dont think you've got what im saying
the set_stdin function should be only a patch function for testing, it should not be published to public
but i hope that i can define in conftest.py
and it doesn't matter cuz im gonna run the test for std input only once among all the other tests

#

but still, i'll check ur code after like, 8 hrs

split field
mortal galleon
#

hi
I am writing a unit test for a function that uses the request library. Ive tried chatGPT and youtube but it just wont work... It seems that the mock values doesnt get passed to the function. This is my loggs:

2023-03-03 06:52:47,214 ERROR Type: <class 'Exception'>
2023-03-03 06:52:47,214 ERROR Details: <MagicMock name='get().raise_for_status()' id='139916181325952'>

When i print response, from:

response = requests.get(url,parameters)                #Issue the GET request
print(response)

I get:

<MagicMock name='get()' id='139916181232800'>

Error:

AssertionError: None != [{'code_review_id': 123, 'title': 'Test code review'}]
#

first time doing this, and im new to python + ive rewritten this class now 100 times. so it might look stupid but here it is:

class TestCrawler(unittest.TestCase):
    @patch('test3.requests.get')
    def test_makeRequest_success(self,mock_requests):
        mock_response = Mock()
        mock_requests.status_code = 200
        mock_response.text = ')]}\'\n{"data": [{"code_review_id": 123, "title": "Test code review"}]}'
        mock_requests.get.return_value = mock_response
        mock_response.raise_for_status.side_effect = Exception('Not found')
        data = makeRequest(url, qParam)
        check = [{'code_review_id': 123, 'title': 'Test code review'}]
        self.assertEqual(data,  check)
#

same if i print status_code:
<MagicMock name='get().status_code' id='139875748252544'>

mortal galleon
#

used responses library instead

ember maple
ember maple
#

also i strongly recommend to never use requests_mock - its a footgun ^^

sharp sparrow
proud nebula
kind meadow
jolly tangle
#

I am pytesting some code in python flask and getting an error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: user.username Can someone help?

Since this this pytesting it is not as obvious solution. Any help is appreciated

Here is the error

https://pastes.io/iijimkme1k

Here is the code

def test_register_page_get(client, new_user, False_output_check_if_user_already_registered):
    
   response = client.get('/register', follow_redirects=True)
    assert response.status_code == 200
    assert b'register' in response.data

    with app.test_request_context(): 
       number_of_users = User.filter_by(username=new_user.username).count()    
       if number_of_users > 1:
               db.session.rollback()        
 
     
        try:   
                db.session.add(new_user)
                 db.session.commit()
            #False_output_check_if_user_already_registered(new_user)
        except:
            db.sesion.rollback()
        else:
            db.session.delete(new_user)
            db.session.commit()

I even tried changing to

 from sqlalchemy import exc
if exc.IntegrityError:
          db.session.rollback()     

instead of

number_of_users = User.filter_by(username=new_user.username).count()    
if number_of_users > 1:
      db.session.rollback()        

in the example above and I am still getting the same error.

ember maple
ember maple
potent quest
ember maple
potent quest
#

will do

shrewd oak
#

@split field Couldn't you inject fake data in stdin instead of setting up a mocked stdin stream?

sharp sparrow
ember maple
shrewd oak
#

Stdin.write?

#

Just like any other IO stream.

shrewd oak
#

Sorry, I had some thoughts about the stdin mocking problem. I think, it would be better to mock readline using unittest.mock.patch instead of monkey patching stdin. Just encapsulate the unit test body into an with patch("sys.stdin.readline", side_effect=[str1, str2, ..., "exit"]):. You can easily combine unittest mocking capabilities with pytest.

sharp sparrow
ember maple
sharp sparrow
ember maple
sharp sparrow
ember maple
sharp sparrow
#

i mean, i can't call a fixture, right?

#

how do i get the input stream object from a fixture?

ember maple
#

Fixtures get passed into functions accepting them

#

Gimme a sec to type that in on the phone

#
def test_input(mock_input) :
  mock_input.send_text(...)
sharp sparrow
#

ok, now what about the output?

#

capfd?

ember maple
#

That may need some research in how to use the output parameter of prompt toolkit, which I cannot do this weekly

sharp sparrow
#

so, as far as i've seen, this prompt_toolkit's pytest fixture is only useful to test only the prompt toolkit stuff

#

and i've no idea how to send the prompt toolkit objects inside the app_sesion to run them all

sharp sparrow
shrewd oak
#

It has already been said: Avoid monkey patching. Remove the get_command_function, the get_command and the replacement_function, use sys.stdin.readline directly and then use @patch or with patch(...):to patch readline.

proud nebula
shrewd oak
#

@proud nebula Monkey pathing???? Were did you read that?

Step one of monkeypatching for tests: avoid

proud nebula
shrewd oak
#

Grmpf. Strictly yes, but the API behind is a little bit more elaborate than what you can with the manual approach.

proud nebula
shrewd oak
#

OK, thats leading to a personal discussion I can't continue today. However, I will look into that 5 min video and maybe my brain gets enlighted after that. Anyway, interesting topic.

weary quarry
# proud nebula Well you should avoid that too imo.

Are you willing to expand on why your flat answers appear to be "do not mock"?

I'm curious to your reasons here. In the majority of experience I've had mocking is an invaluable tool. The only downfall I've found is that some tend to be looser with their test code than their implementations, causing issues. Smells in those cases are, generally, caught and corrected in review.

proper wind
#

here's a very simple question, if it doesn't belong here let me know. I'm testing a really big database-making program for my Object-Oriented Programming class (which is in python for some reason?) and it takes forever to put the first dozen or so inputs into pycharm over and over

#

can i automatically program a specific set of user inputs to start it off so i can test the later things without having to write the same stuff over and over?

autumn bronze
#

If you're taking stuff through input() and can't be bothered setting up pytest, you can just put the text on different lines in a file and pipe it into python

#
cat file_with_inputs.txt | python3 script.py

On Linux like systems, and you can probably find a windows equivalent

maiden pawn
proper wind
autumn bronze
#

Sure, like I said this is to do it without changing much in the script

#

There's obviously better architectural changes that can be made

maiden pawn
maiden pawn
autumn bronze
#

I don't disagree, I was just providing a "get it working" approach

proper wind
#

oh this is like 500 lines of code and i am new to python

#

is pytest gonna need like a ton of rewriting

maiden pawn
#

Ergo... Inputting from file is not clean. Mocks are cleaner more precise surgical instrument

maiden pawn
proper wind
#

can i like set a given list of inputs that it'll take and it'll just run through the next one for each input()?

maiden pawn
#

With unittest.mock? Yes

proper wind
#

ok cool

#

it's not like an application, it's a program in terminal?

#

ok yeah

maiden pawn
#

Feel free use default unittest instead of pytest.. If u work without libraries

proper wind
#

yeah i'm not getting that advanced anytime soon

#

oh if i can just use unittest yeah

#

ok this seems like much more in-depth testing stuff for individual variables: is there a way i could just implement this in a normal run of the code?

maiden pawn
#
def input_generator():
  yield "ABC"
  yield "123"

iterator = iter(input_generator())

def input():
  return next(iterator)
#

This code will replace Inbuilt input with input returned in sequence from input generator

#

Technically... If would be shorter and better done in unittest.mock

#

But i am at phone, and can't remember from the top of a head how to do it with mock 😁

proper wind
#

ok, thank u so much!

#

wait what if i just want it to do it for a certain number of inputs and then after that i put them in

shrewd oak
#

There is this discussion about using (monkey)patch or not. However, none provided a non-patch alternative. I'm curious, too.

proud nebula
fickle solar
#

I have 3,000 test cases. Each one is a page of text. Do people typically use SQLite to store these?

shrewd oak
#

My test cases are usually within a source tree.

proud nebula
#

because what you said doesn't make sense with the definition I'd use

fickle solar
#

A page of text from a document. The expected result from a page is a list of tuples.

fickle solar
shrewd oak
#

@fickle solar I think we're talking about different dimensions.

#

... And for me a test case is a bunch of code not a bunch of test data.

#

Which means the bunch of code is there to test something specific and orthogonal about something under test.

brazen needle
#

If you're really planning to have several million of these (but how are you planning to make them? This part doesn't make sense to me), then I doubt you want to put them all into your source tree. But you're going to want to keep them under version control. You could consider having two separate source trees, one with all your code, and one that simply holds test data. You could still arrange your test data as individual files as long as you can think of a hierarchy of some sort (even if it's just numbering: Test inputs 0 to 999 go in 0, test cases 1000 to 1999 go in 1000, etc.).

#

If you take that approach, you will need to be very careful about how you set up testing to make sure that you run all the tests you're supposed to be running.

proud nebula
fickle solar
#

Maybe. It's a machine learning algorithm that extracts information from text.

#

We need to make sure it meets a threshold accuracy on the dataset we have, so that we can report it to clients.

#

It's not all machine learning, and when we update the algorithm we need test inputs to see if there's regression.

brazen needle
#

It sounds to me like you need two different testing regimes, then. You need conventional testing to make sure that your code functions the way it's supposed to (doesn't crash, handles invalid inputs gracefully, etc.). But you also need algorithmic test cases to ensure that your algorithm functions correctly, and those are what you're asking about. Is that right?

#

And I'm guessing that you have some method for generating test cases and known correct output, and that's why you can plausibly have millions of test cases.

fickle solar
#

That makes sense yeah. Thanks guys.

supple flume
#

I've heard the tear unit testing a lot but what does it actually mean

maiden pawn
maiden pawn
supple flume
#

is it a complicated topic

maiden pawn
supple flume
#

why is called unit testing

maiden pawn
#

ensuring its expected working according to written test specifications

supple flume
#

Wait you can do ?

#

with an IDE or with code

maiden pawn
#

both 🙂

#

but well, with code first

supple flume
#

that seems helpful

maiden pawn
#

you can run tests from console, or from IDE too 🙂

#

and having visual debugging interactive process during their run if you want (when running from IDE)

supple flume
#

thank you for the explonation

#

unit testing seems very useful

maiden pawn
#

just pressence of unit testing = jumps your code quality by three times 🙂

#

if you think how you are going to test your code before you write working code itself => Quality is doubled and tripled in addition

supple flume
#

so I have to write the tests myself

#

or import a module

maiden pawn
supple flume
#

got it thx

maiden pawn
#

while importing module for unit testing 🙂

#

unittest in python is default... but usually if you install any third party modules, better testing with pytest

brazen needle
#

pytest is just better than unittest. It's easier to use and has more features. I wouldn't recommend unittest to anyone at this point.

maiden pawn
#

pytest is backward compatible with all unittest stuff anyway

#

unittest.mock is splendidly awesome, and makes sense to use with pytest too, instead of pytest obfusactions for this

weak drum
#

using robot framework, but unable to get any output although i set the logfile to sys.stdout; verified this works when calling on the class it self ```python
import pexpect
import sys
from robot.api.deco import keyword

class main:
def init(self):
child = pexpect.spawn("/bin/bash", encoding="utf-8")
child.logfile = sys.stdout
self.child = child

@keyword    
def ls_cmd(self): 
    self.child.sendline("ls -la")
    self.child.expect("#")
    
@keyword    
def cleanup(self):
    self.child.close()
shrewd oak
solemn flint
#

Hi, can some one help to find understanding in mock functions?

@patch('report_generate.build_report',  return_value="some_data")
def test_init(mock_build_report):
    assert build_report('path_to_filese_for_report_building') == "some_data"
    mock_build_report.assert_called_once()

perhaps there is some description besides the official documentation, for easier understanding

proud nebula
solemn flint
proud nebula
tacit crown
#

Python3 unittest import hell

Have scoured the usual sources and can't find a solution that works that doesn't smell.

My project directory structure:

project/
    src/
        a.py
        b.py
    tests/
        tests_1.py
        tests_2.py

File a.py imports b:
import b

tests_1.py and tests_2.py need to import a and b:

import unittest

from src import a, b

What's the "best practice" way to get this to work without smelly hacks?

uneven beacon
proud nebula
tacit crown
# uneven beacon shouldnt that just work? a gets imported which imports b, then b gets imported w...

It doesn't "just work".
Either imports into the tests works but complains that "b" can't be imported,
or,
imports into a.py works but no tests are found.

This stackoverflow question covers the same topic, https://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure , but as the later comments to the accepted answer say:

This won't work if antigravity/antigravity.py imports from a module under the same antigravity directory.

proud nebula
proud nebula
tacit crown
proud nebula
#

I would move a and b up a level and delete the src dir

#

Then in the same directory as a, b, tests folders, you can just type pytest and everything will work by default

tacit crown
#

You mean like this?

project/
    a.py
    b.py
    c.py
    ....py
    readme.md
    LICENSE
    .pylintrc
    project.ui
    .gitignore
    tests/
        test_1.py
        test_2.py
        test_n.py
    docs/
        build/
        source/
        Makefile

Isn't it rather messy to have the project's python files mixed up with so much other stuff? I don't see many Python projects doing this other than very small projects.

tacit crown
#

I do appreciate your help @proud nebula but I've been going round this ballpark for 2 days and I'm astounded that there are so many recommendations to put the project's .py files into one folder, tests.py files into another, but no definitive way to make that work with unittest.

proud nebula
tacit crown
proud nebula
proud nebula
#

Also, use a good project structure :P

#

You want to have just one dir with your projects stuff in it in the top level folder anyway. You shouldn't have a.py and b.py, you want to namespace your stuff like your_project/a.py

#

Only exception is if your entire project is literally one .py file. Like polib has that for example.

tacit crown
proud nebula
#

This is idiomatic python.

tacit crown
#

from myproject.a import some_symbol in a.py, b.py and test_n.py?

proud nebula
#

It will be the same syntax in all files yes

#

I only use import x type imports in some VERY rare cases. It's better imo to stick to from x import y style imports always

solemn flint
proud nebula
#

yea, don't forget the __init__.py file

#

in both tests and myproject dirs

tacit crown
proud nebula
proud nebula
solemn flint
proud nebula
#

if you have a __init__.py in a dir, it means it's a python module. That's about it. Not much more to know.

tacit crown
#

I've found the search term Namespace packages - this looks promising.

solemn flint
proud nebula
tacit crown
#

Regular packages are traditional packages as they existed in Python 3.2 and earlier.

Do "regular packages" exist in Python 3.9 and later?

uneven beacon
tacit crown
uneven beacon
#

i.e. py src/ a.py import b b.py tests/ test_1.py import a pyproject.toml sh /myproject $ pip install --editable . /myproject $ pytest

tacit crown
uneven beacon
#

the point of an editable install is to let you work on the project (without reinstalling / tweaking pythonpath) while allowing your imports to be written in the same way they should be when installed

tacit crown
proud nebula
proud nebula
tacit crown
uneven beacon
proud nebula
proud nebula
#

simple is better than complex

uneven beacon
#

and both are still simple layouts

proud nebula
#

One approach is generally useful to understand how python works, the other is not.

proud nebula
uneven beacon
#

well the links i provided already provide arguments for and against its usage in projects

proud nebula
tacit crown
proud nebula
#

No config file.

tacit crown
proud nebula
tacit crown
proud nebula
#

"a.py" isn't "an app". It's a file. Your "app" is probably my_project.a at this point

proud nebula
#

also, you should get in the habit of showing full errors

uneven beacon
# tacit crown I've tried several. How *should* I be launching the app ("a.py") so that it find...

also worth reading this to better understand the import mechanisms https://docs.python.org/3/library/sys_path_init.html
to summarize the relevant parts:

  • sys.path dictates where to search for modules when doing absolute imports
  • by default, this includes python's built-in modules and the site-packages directory, containing your installed packages
  • when running a script normally (i.e. python main.py), the directory containing that script gets searched before the others
  • when running a module as if it was a script (i.e. python -m pytest), the current working directory of your terminal gets searched first

given the above facts, there are two (conventional) ways to go about it depending on your project layout:

  • in an src-layout, the src/ protects mypackage from being found unless it is installed, so the workflow would be to install the package first, then run your commands like normal: py src/ mypackage/ __init__.py a.py from .b import foo b.py tests/ test_a.py from mypackage import a ```sh
    pip install -e .

open an interactive console with mypackage imported

python -ic "import mypackage"
pytest- in a flat-layout, it is possible for you to import the package from the project root without installation, although it requires your scripts to search the project root for your package:py
mypackage/
init.py
a.py
from .b import foo
b.py
tests/
test_a.py
from mypackage import a sh
python -ic "import mypackage"

run pytest with -m so the current working directory (which includes mypackage)

is available to your tests

python -m pytest```

tacit crown
# proud nebula "what did you do to get the error?"

For clarity, I've created a minimal test project.

Test\
    my_project\
        a.py
        b.py

The contents of a.py:

from my_project import b

def main():
    print(b.my_add(2, 3))


if __name__ == '__main__':
    main()

The contents of b.py:

def my_add(x, y):
    return a + b

I want to run a.py.
My cwd is .../Test/

How should I run a.py ?

proud nebula
#

You don't really "run a.py". You run the module my_project.a.

tacit crown
# proud nebula `python -m my_project.a`
$ python3 -m my_project.a
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "<full-path>/Test/my_project/a.py", line 14, in <module>
    main()
  File "<full-path>/Test/my_project/a.py", line 10, in main
    print(b.my_add(2, 3))
  File "<full-path>/Test/my_project/b.py", line 8, in my_add
    return a + b
NameError: name 'a' is not defined
uneven beacon
#

your parameters are named x and y

tacit crown
proud nebula
#

Don't confuse him further :P

uneven beacon
uneven beacon
#

sure it would

proud nebula
#

ugh

#

python is a such a mess

tacit crown
#

I've added to my Test project:

Test\
    my_project\
        tests\
            test_1.py

And in test_1.py:

import unittest

from my_package import a, b

class TestIt(unittest.TestCase):
    def test_01(self):
        print('Hello World')

    def test_02(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()

Then I try to run:

$ python3 -m unittest discover tests

which returns the error:

E
======================================================================
ERROR: test_1 (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_1
Traceback (most recent call last):
  File "/usr/lib/python3.10/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.10/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/<full-path>/Test/tests/test_1.py", line 3, in <module>
    from my_package import a, b
ModuleNotFoundError: No module named 'my_package'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
proud nebula
#

that makes no sense

#

why in gods name does python add the directory of the file FIRST in sys.path?!

proud nebula
uneven beacon
#

ive seen that trip up plenty of people who named their discord bot discord.py then tried writing import discord

proud nebula
uneven beacon
tacit crown
#
import unittest

from my_project import a, b

class TestIt(unittest.TestCase):
    def test_01(self):
        print('Hello World')

    def test_02(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()

which returns:

$ python3 -m unittest discover -t tests
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.10/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/usr/lib/python3.10/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python3.10/unittest/main.py", line 124, in parseArgs
    self._do_discovery(argv[2:])
  File "/usr/lib/python3.10/unittest/main.py", line 244, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
  File "/usr/lib/python3.10/unittest/main.py", line 154, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "/usr/lib/python3.10/unittest/loader.py", line 346, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: '/<full-path>/Test'

and it's the "-t" that's wrong...

#
$ python3 -m unittest discover tests
Hello World
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Looking good...

proud nebula
#

Now, if you want to switch to a src style directory you have a baseline to work from here.

uneven beacon
#

i havent used unittest that much but according to the docs, it seems like you'd want the start directory to be tests but the top-level directory to be your project root (assuming a flat-layout where you havent installed the package)

tacit crown
#
import unittest

from my_project import a, b

class TestIt(unittest.TestCase):
    def test_01(self):
        self.assertEqual(b.my_add(5, 6), 11, "Should be 11")
    def test_02(self):
        self.assertEqual(b.my_add(6, 6), 11, "Should be 12")

if __name__ == '__main__':
    unittest.main()

$ python3 -m unittest discover tests
.F
======================================================================
FAIL: test_02 (test_1.TestIt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/steve/Code/Python/Test/tests/test_1.py", line 9, in test_02
    self.assertEqual(b.my_add(6, 6), 11, "Should be 12")
AssertionError: 12 != 11 : Should be 12

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

Hooray!
Thanks very much guys. I can take it from here.

uneven beacon
#

ah right -m is adding the project root for you

tacit crown
#

And interestingly, __init__.py was not required anywhere - so now to make a cup of tea and read up on Namespace packages.

#

Thanks again.

rare atlas
#

so I changed a program i made to use custom exceptions, both of which inherit from ValueError, and i ran pytest expecting to see a whole bunch of failing tests. But it turns out with pytest.raises(ValueError) also accepts ValueError subclasses. TIL

#

I don't want to in this case, but is there a way to prevent that?

maiden pawn
# rare atlas so I changed a program i made to use custom exceptions, both of which inherit fr...

pytest captures the exception in an ExceptionInfo object. You can compare the exact type after the exception.

def test_get_holidays_raises_ioerror():
requests.get.side_effect = IOError
with pytest.raises(IOError) as excinfo:
get_holidays()
assert type(excinfo.value) is IOError
https://stackoverflow.com/questions/57160323/in-pytest-how-to-assert-if-an-exception-which-is-inherited-from-a-parent-excep

The answer is not wrong, but the additional assert has the same effect as an assert True. pytest.raises(IOError) will fail the test if another exception or no exception is raised, so the assert line won't be reached anyway. If you want to test for exceptions subclassing IOError, the assert will be reached, but will duplicate the type check already made in RaisesContext. –

rare atlas
split field
fiery arrow
#

I read PEP420 way too many times and I think at this point my brain has developed immunity to understanding it 💀

tacit crown
# split field there are cases where you can get away without having a `__init__.py`, but you s...

It's not really a matter of "should" or "getting away with" __init__.py. Sorry, but it was because of dogmatic statements without adequate explanation, that I was confused in the first place. The presence or absence of __init__.py is largely irrelevant to the import problem that I was asking about.
Where __init__.py was relevant, was that support for Namespace packages in Unittest was broken for a long time before it was officially removed. During the years that it was broken, many "answers" were posted on-line for ways to work around the problem, frequently based on what worked in Python 2. The result: a confusing mess of bad and outdated advice and rules.
The real answer is as given in the Test Discovery section of the current Unittest documentation (https://github.com/python/cpython/blob/main/Doc/library/unittest.rst#test-discovery), which I would summarise as: everything must be importable from the top-level directory of the project.

ember maple
#

a caveat with pytest is that we have a pre-pep420 mechanism as of now, so __init__.py files are needed for sanity details

digital pivot
#

Is there any way to make pytest spawn a new process/reset everything for every test_module? Context - we are testing some applications which are in a monorepo, and all designed to run seperately from each other. We are using respx to mock endpoints, and finding that if we (for example) spawn a thread in module_1.py which was hitting a mocked api endpoint every few seconds, it could interfere with the tests in module_20.py.

There are things we can do to get around this, but the simplest would be to have pytest spawn a process/reset everything per test module. Other than just invoking pytest for each test_module, is there anyway around this?

proud nebula
digital pivot
#

using respx to setup the routes, with defined return values

tall brook
#

I haven't used respx but an idea could be to run each test in independent containers. Or perhaps dig deeper into respx docs and see how those scenarios are addressed

ember maple
distant horizon
#

Hi all! Does -s work anywhere that you might want to use log_cli=true? I.e. with -s is there any need for log_cli in the config?

distant horizon
ember maple
distant horizon
#

@ember maple Is there any reason why log_cli=true would print to stdout but -s wouldnt? And if so, is there a cli option/arg to provide to enable log_cli=true?

ember maple
distant horizon
#

No the project I work on just treats everything at info level or above as a glorified printf and anything in stdout gets skurpeed up by the log collector.

We add some structure to it as well but we don't use any of the machinery that one might use to manage log levels for individual components.

distant horizon
#

We see a weird behaviour basically where in pycharm an improperly mocked celery task complains about not being able to access rabbit in the test if we use log_cli but not -s. And from the command line pytest cmd we can't get the same error to print even though we know it has the same problem as it's suffering from the same timeout behavior

loud flame
#

Hi all, I just discovered this discord. For background, most of my python code has been in Jupyter Notebooks (so please forgive my ignorance about unit testing tools in python), but I've recently needed to create a project that will be packaged and deployed with pip within my organization. The package uses an API for a cloud file storage/sharing platform, and I'm attempting do proper unit testing since it'll be used by others. I'm using pytest and unittest.mock for this and most things are testing well. I'm struggling with some of the mocking concepts related to how to set and assert mock objects attributes or method return values.

In my test code, I attempt to use @patch decorators to replace the external API calls. Specifically, there are two decorators in the testing scripts for each function. One for authentication to the API and another which returns a client object that should be returned by the authentication object's constructor. For example:

auth = APIAuth(someargs)
client = APIClient(auth)

So, I want to mock both the auth and the client objects for a test function. Within the test code, I can successfully do this: mock_auth.assert_called_with(someargs), but mock_client.assert_called_with(mock_auth) fails and shows that mock_auth's ID and the code under test's value for auth are different mock objects.

#

This is fairly important for my further testing, as the "client" object returned has many methods that are used in my program, and I need to be able to set various return values and side effects within that object to properly test things. So later in the program (and in other test scripts) this can happen:

folder = client.folder(folder_id=somearg)
newfile = folder.upload(somefilepath)
print('ID: ' + newfile.id)

And in my test, I want to set the return value of mock_client.folder().upload().id()'s return value to some bogus string and assert 'bogusstring' in result.output

Am I way off base in my efforts here? Could someone give me a hand or a nudge in the right direction? Thanks!

proud nebula
loud flame
#

...same as if I were trying to mock a database or other http request like so many unittest.mock examples around propose.

earnest sequoia
#

wrap the API in a proxy class. then you can mock your proxy class with another class that implements the same interface

maiden pawn
digital pivot
sweet notch
#

Is this crazy to have a fixture for my unit tests and fixture for integration tests that calls my unit test fixture?

@pytest.fixture(name="persisted_supplier")
def persisted_supplier(supplier_instance):
    db.drop_all()

    db.create_all()

    db.session.add(supplier_instance)

    db.session.commit()


# Unit fixtures
@pytest.fixture(name="supplier_instance")
def supplier_instance() -> Supplier:
    """
    Return a Supplier instance with sensible defaults for testing
    :return: Supplier
    """
    supplier = Supplier(
        id=1,
        name="dev_test_supplier_one",
    )
    return supplier```
#

idea is I call the supplier_instance for testing the model and then a persisted_supplier when I need to test the route.

ember maple
sweet notch
#

I need a supplier instance for model testing without persisting that data and then a persisted supplier for route testing.

#

That for the feedback here I need to do some thinking

#

I may just make both fixtures have no dependencies and instantiate a supplier in persisted_supplier function.

ember maple
sweet notch
#

I was debating looking in to factories also

ember maple
sweet notch
#

Thanks for the direction dude, do you mind if I share my project with you later would love some feedback?

slow vessel
#

title: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
why does it let me insert empty rows even though nullable is false?

ember maple
sweet notch
#

Thanks dude its a small project and I really appreciate your guidance!

#

will be sure to reflect what we have discussed in the project before I send

spring veldt
ember maple
#

to add salt, i gave the "deceptive" code to chatgpt as well and it wrote correct unittests and explained why it ignored deceptive parts of the code

spring veldt
maiden pawn
# ember maple i gave this a short test, the tests generated are extremely chatty with a high n...

@spring veldt i already tested a similar extension before and my final TLDR was next one

good - generates new ideas, like you have a partner in paired programming
bad - bad namings like novice programmer, not holding meaning why the test is needed
bad - too tightly coupled code tests to implementation details, making difficult continued development (your program becomes rigidly fixed to the current implementation, if u will try refactor it, you will start receive a lot of broken tests when they should not have been broken, potentially quite quickly leading to dissapointment of team of using tests in general)
bad - rips away thinking about code architecture of a program. Testing is important is exactly for this reason, to think about testable code architeture.
as you can see from this conclusion, this tool can get you short gain quickly (in covering current code with tests more), but the bad things... for majority of developers can rip away future of products they develop.

spring veldt
maiden pawn
maiden pawn
#

Hi there. I made sure to check your video and to analyze what you are offering as your tool.
Sorry for being late. A bit busy previous week i had. Hehe, was finishing my pet project too.

lets divide into what i like, what i dislike regarding your tool:

  1. i like that your tool allows... to... generate a bunch of code, in different ways testing working solution.
    Surely great to generate ideas where you missed your code.
    I am not exactly sure only regarding... amount of spent time to carefully distinguish useful bits from not useful ones in this good spot.

  2. i dislike that generated tests do not hold... mm meaning in test names, that holds why they are important?
    let me give you example. Imagine we asked intern-novice-junior programmer to comment what is this code doing?

for i in range(10):
  print(f"i={i}")

What we have high chance to receive?

this code does loop from 0 to 10, and prints i=values at each new string

for i in range(10):
  print(f"i={i}")

Absolutely exact description, and absolutely useless one. We don't know what this code doing, and comment just repeates what this code does in exactly repeating each encountered syntax, variable name and language action.
I see that so far tests generate have pretty much similar value

#
  1. i potentially dislike also next thing: essentially the purpose of TDD is not about writing tests.
    It is about making you thinking how to write code architecture in the way, that your code would be testable.
    I am afraid that using this tool potentially propagates bad practice of not thinking about it. Thus eliminating pretty much all meaning of TDD.
    • i dislike next thing also: writing tests should be as important as writing main code of a working solution.
      each test should be documenting some domain knowledge of our tool, and also we need to make them in the way, that they would be as least breaking code as possible. Through writing tests... in carefully chosen.... places where tests preferably test only external behavior of code places, and leave open room for easy refactoring code with the least amount of test fixes that would be needed after that.
      Tests written in the way they break as least as possible, allow us to write things with relience on written tests, and being confident we can easily fix them to next close commit, having them all passing green and feeling safe regarding our code.
  • Your tool facilitates overtesting of code implementation details in a very... over thorough manner too much close to implementation details. Thus making code rigid-fixed by all those generated tests. It can bring to developer disappointment, that all tests are very quickly breaking when he tries to add new features or refactor code to new state.
    ====
    So, 2 potential likes for things good to have, and 3 things i potentially dislike regarding it.
    To say to for sure more about it, i would need to implement small project with using the tool, but i don't have that much time to invest for now.
#

good - generates new ideas, like you have a partner in paired programming
good - potentially increasing Dev speed
bad - bad namings like novice programmer, not holding meaning why the test is needed
bad - too tightly coupled code tests to implementation details, making difficult continued development
bad - rips away thinking about code architecture of a program
as you can see from this conclusion, this tool can get you short gain quickly, but the bad things... for majority of developers can rip away future of products they develop.

maiden pawn
#

when developers thinks about how to test his code, he refactors it to make code architecture more testable

#

if we are just having auto generated unit tests, we have majority of meaning behind unit testing killed as well

spring veldt
# maiden pawn if we are just having auto generated unit tests, we have majority of meaning beh...

@maiden pawn Thanks for the elaborated explanation. We are planning to provide certain customisation settings to allow developers feed in their inputs and insights. This will ensure that the testable architecture is in place. We also plan to launch settings that would allow refactoring based on the inputs of a developer.
Currently, our tool is focusing on reducing the manual effort of developers.

spring veldt
ember maple
spring veldt
#

@ember maple We don't think the platform has made any mistakes while creating unit tests for both simple and complex codes. This is as per our rigorous testing and user feedback from various other communities. In case you have found any mistakes, I would request you to share the screenshots of the results given by our machine.

spring veldt
ember maple
#

Based on my initial tests, the feedback and the replies it's not worth my time to do a free quality qa for them,

cerulean cradle
#

what are the dependencies for unit testing?

#

new to python*

brazen needle
#

pytest is popular.

cerulean cradle
#

what is a test case id

raven narwhal
#

69

spring veldt
drifting sorrel
#

I would like to share a workflow I use on a daily basis when writing Python code: REPL Driven Development. I don't think that it is a common thing among Python developers (yet), but think it is very useful for developing testable code (and a good starting point for writing unit tests). If you haven't heard about it before: it is not about firing up a REPL and writing or importing code or scripts in there. Instead, you evaluate variables, code blocks, functions - or an entire module - without leaving your favorite code editor. The good part is the instant feedback. I use Emacs, and the elpy package has a good amount of features already in place for this kind of workflow - in combination with the powers of IPython. I have also tried this workflow out in PyCharm and VS Code with success. I have written about it, and you'll find more details here: https://davidvujic.blogspot.com/2022/08/joyful-python-with-repl.html.

maiden pawn
#

i can see it is as a good poor man choice if person hasn't learned how to do unit testing yet though

#

after all, Jypiter with its ability to write in its embedded repl exists too

#

Like.. there is no Point to specify Repl Driven Development, because during unit testing writing in IDE, we already use it (It is just called enabling interactive visual debug), and we already have this ability to execute code on a fly in Repl, except all code is already initialized for specific situation

drifting sorrel
#

My view of it is more like Test Driven Development Deluxe - with very fast feedback loops. A common thing is to actually write some "scratch style" code next to the actual code to test. These ones are easily converted into proper unit tests if you think it add value to keep.

maiden pawn
drifting sorrel
#

It's not really debugging. You evaluate the code, without the context switch of writing something in a separate REPL (or even a unit test).

maiden pawn
#

you mean, you build each time this context from scratch? :/

drifting sorrel
#

Not following you there 🤔

maiden pawn
# drifting sorrel Not following you there 🤔

You evaluate the code, without the context switch of writing something in a separate REPL (or even a unit test).
so to execute the code, u need to initialize everything necessary for its execution from scratch

#

and u have bunch of arbitary code you don't know which (because not recorded as a code), that builds your repl environment

drifting sorrel
#

No, not at all. You have a session "alive" and you evaluate - and re-evaluate - code blocks as you type.

maiden pawn
#

ergh, essentially your suggestion is just using

if __name__=="__main__":
  my_scaffolding_code_i_write()
#

there is nothing new in this approach

#

it is a regular novice approach before they learn how to unit test code

drifting sorrel
#

hahaha, okay. I didn't say it is new, but probably not that heard of in the Python community.

maiden pawn
#

or if some very fast simple script is cooked up, too simple for proper written application code

drifting sorrel
#

I suggest you to read the blog post and check out the 5 minute video in there - that might clear things up for you a bit.

maiden pawn
#

technically it is not unit testing at all, and this discussion is not supposed to be there pithink

drifting sorrel
#

I believe it is about testing and according to the description in this channel I think it might be appropriate to share here.

maiden pawn
#

but it does not test code :/ tests are documentation to the code
your approach is equvalient to explorative tests which are written temporally, except your stuff is always deleted as a must

#

tests are supposed to be repeatable code written how to perform them

ember maple
#

Repl driven is good for experimentation, but it's very easy to miss necessary write the real tests cutoffs

And then it turns into self sabotage

drifting sorrel
#

Can you elaborate a bit on that? I don't see doing one or the other?

ember maple
#

A set of messy jupyter Notebooks ain't gonna turn themselves into a testsuite plus we'll factored code

drifting sorrel
#

Just to be clear: I would call myself a TDD:ist everyday of the week. Wrote my first Unit tests in 2005 correction: 2004 - and have not let go of it since.

#

I don't think REPL Driven Development is about Jupyter Notebooks - where does that come from?

ember maple
drifting sorrel
#

I think that you have misunderstood this thing - it's not about writing code in a REPL env. You would write the code in your editor, just as I described in the initial post, in the linked blog and demo in the 5 minute video you'll find in there.

ember maple
#

Python isn't exactly live coding save language, i had major pains with anything that pretends it can safely reveal partial snippets of code from the editor

drifting sorrel
#

I would encourage you then to try out REPL Driven Development - not much pain there.

#

And to be even extra clear, in case you think so: I am not suggesting anyone to replace unit tests or throwing away pytest at all.

ember maple
drifting sorrel
#

Have you even tried that out? I have developed Python for a bunch of years, and been practicing REPL Driven Development with the combination of writing unit tests for about 2-3 years.

#

I have yet not seen code break that often 😏

#

This kind of thing has helped me write code with better quality, and in combination with unit tests (especially TDD-style) it is a nice tool to have in your toolbox as a developer. I wanted to share that.

ember maple
drifting sorrel
ember maple
drifting sorrel
#

Totally agree 👆

ember maple
#

Also wrt mocks/mocking, massive amounts of it is toxic (so many people mock things they neither own nor understand)

drifting sorrel
#

I recognize that, and when reflecting on those kind of tests I think a huge part is production code that does too much - and the unit tests with those huge amounts of mocks become more of integration tests. That's where TDD and RDD can do great things. So, in a way TDD (and RDD) is more about software design than actual testing.

ember maple
#

Hmm, if we consider this as a spectrum between running the testsuite, running a debugger on failed tests and running code examples for exploration, there may be some better to agree on middle ground

ember maple
drifting sorrel
# ember maple IMHO one of the issues that leads to such production code is that the starting p...

My experience is quite the opposite (if I understand you correctly) - starting off writing code, and then writing some tests. That workflow will often lead to painful sessions of patching and mocking code. The RDD way I suggest is very much like TDD - you write code, evaluate, try out, refactor, re-evaluate ... with that workflow - that shouldn't be too awkward for test-driven developers at least - will increase the chances of ending up with production code that is testable and that doesn't require those nasty amounts of mocking.

ember maple
drifting sorrel
#

Sounds like we're on the same page there!

ember maple
#

In essence you describe a lisp style repl, which is most powerful on top of a language with safe code reload and symbolic references

#

And the example size is basically something one would drop into a test function or a jupyter notebook cell

drifting sorrel
#

In such an environment (Lisp) you actually modify a running program itself - a very powerful thing to have as a developer. NRepl is a very powerful tool and I would very much like to have something like that in Python too.

ember maple
#

I think we have established that for python there is a number of footguns for repl driven development, we just seem to be blessed with different hit rates

drifting sorrel
#

Why write something like that? 👆

ember maple
#

It would be nice to have a python language server that could do symbolic replacement in the targeted interpreter

brazen needle
#

I have used "REPL-drived development" quite successfully sometimes. I find it's most useful to me when I'm thinking, "What am I even doing here?"

#

(I use the vim-slime package for this.)

#

I see it as complementary to unit testing (and other kinds of testing). I generally find myself using it on very small snippets.

#

It is often the case that these small snippets become functions which get tested properly. That's a step that you can't ignore (unless you're writing a throwaway script). At some point, it becomes more useful to write proper unit tests for these functions.

#

But sometimes it happens that I have only a vague idea of what the function is supposed to do. Maybe I know it's some kind of utility function for a larger piece of code, and there might be more than one of these, and I'm not sure quite what responsibilities each should have. Trying to implement things can make a better architecture clearer. This is the sort of situation where I find myself using a REPL for development.

#

It lets me develop enough to feel confident that things are structured reasonably and that they handle some inputs correctly. It's not a finished product, but it's progress.

#

Some people like to use Jupyter notebooks for this sort of thing. I generally dislike notebooks; I find it too easy to create bugs by executing cells out of order. (I feel like notebooks can be excellent for writing tutorials, though. I have done that successfully.)

cerulean cradle
#

if name == 'main':
unittest.main()
print ("Everything pass")
it says name is not defined..
any suggestions ?

bitter wadiBOT
#
Formatting code on discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

proud nebula
#

ah

#

..weird. Why can't I put a 👍 on your !code comment?

#

Funny though, the entire Discord window shakes to say I am not allowed to do that.

wicked vigil
proud nebula
novel canopy
#

I must agree with boxed

drifting sorrel
#

I haven't tried it out, but think the idea of an AI assisting by writing unit tests is pretty cool. I see use cases as something similar to a pair-programming (Human-AI) or a code-vs-test pingpong session for developing code. I haven't looked into that ChatGPT stuff yet, but applications of it like this would make me more interested than chatting.

pseudo ermine
#

I want to unit test a method of a class that calls a bunch of GUI methods in its constructor. If a create an object from that class the programm will crash (Its a crash in native C code so I cant catch it).

I dont really need the constructor to be called anyways, is there a way to just test the method without calling the constructor?

drifting sorrel
#

How about extracting the contents of the method into something that isn’t wiring up state? A function, or a separate class?

ember maple
#

As mentioned extraction may be the easiest approach for better suggestions details on the gui toolkit is needed

upper patio
#

Hi there. I'm working on an desktop automation project, using PyWinauto library. Can you imagine a way to create unit tests for classes/functions whose purpose is interacting with a GUI? I'm bad at testing, but afaik, the approach is to separate the logic that performs concrete actions like for example searching the window to find a specific element and click it. I'm unable to even think of was to do that. Does it even sound realistic?

solemn solar
drifting sorrel
solemn solar
drifting sorrel
solemn solar
proud nebula
solemn solar
#

Hh they're all great tools, I use neovim as well, just trying to make the best use of them.

drifting sorrel
oblique shale
#

!rule ad

bitter wadiBOT
#

6. Do not post unapproved advertising.

proud nebula
#

<@&831776746206265384>

minor furnace
#

remove this

#

thanks @proud nebula

crude marten
#

I'd like to make a correction of this package : https://github.com/LAMDA-NJU/Deep-Forest
I tried to run test with pytest which returned me a first error :

Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_tree_same.py:24: in <module>
    from sklearn.datasets import load_iris, load_wine, load_boston
/opt/homebrew/lib/python3.11/site-packages/sklearn/datasets/__init__.py:156: in __getattr__
    raise ImportError(msg)
E   ImportError: 
E   `load_boston` has been removed from scikit-learn since version 1.2.
E   
E   The Boston housing prices dataset has an ethical problem: as

I corrected it with fetch_california_housing not sure if it works for now.
But then I ran into another issue :

ImportError while importing test module '/Users/tools/Deep-Forest/tests/test_forest.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_forest.py:3: in <module>
    from deepforest import RandomForestClassifier
../deepforest/__init__.py:1: in <module>
    from .cascade import CascadeForestClassifier, CascadeForestRegressor
../deepforest/cascade.py:21: in <module>
    from . import _io, _utils
../deepforest/_utils.py:7: in <module>
    from . import _cutils as _LIB
E   ImportError: cannot import name '_cutils' from partially initialized module 'deepforest' (most likely due to a circular import) (/Users/dpratella/tools/Deep-Forest/deepforest/__init__.py)

I don't how to deal with this error.
I'm trying to work on the "test_forest.py"

Any help would be welcome !

#

And basically I just cloned it from github and ran pytest test_forest.py

earnest sequoia
#

where the developers would actually be notified about it

crude marten
#

thought It would be faster for me to correct it by myself

earnest sequoia
#

oh, sorry I think I misunderstood your intention (when you said "correct", I thought you were "correcting" the devs xD)

#

you should check that your test script is run from the top level of the package

crude marten
#

I tried both :
pytest tests/test_forest.py
pytest test_forest.py

And still get the same message

earnest sequoia
#

hmm not sure then

dense garnet
wraith forge
#

I'm having issues mocking reading a binary file if anyone is able to assist!

proud nebula
proud nebula
wraith forge
#

mb started to write one and got distracted

#

I'm writing a unit test for a function that opens/reads a binary log file but have not been able to get the mocker.mock_open function to work

#
    def test_read_log(self, mocker: MockFixture):
        """Check reading logs"""

        data = bytes(b"\x01\x02\x03\x04")
        data += bytes("0", "utf-8")
        with mocker.patch("builtins.open", mocker.mock_open(read_data=data)):
            with open("tempfile", "rb") as tempfile:
                log = blp.read_polling_log(tempfile, data_header_offset=0)

        assert log[0].body_string == "empty"
        assert log[0].record_body_len == 0
#

here's my code, it throws an AttributeError: __enter__ at the with mocker.patch line but i'm not sure what that means

proud nebula
#

It's unintuitive and brittle. That's why mocking should be avoided. Well. One good reason.

wraith forge
#

i mean if there is a better option i would be happy to do it instead but this is the only way i know of to make this test

proud nebula
wraith forge
#

do you mind if i dm it to you?

dense garnet
bitter wadiBOT
#

tests/laws.py line 25

def lattice(join): return [associative(join), commutative(join), idempotent(join)]```
proud nebula
wraith forge
#

I got it figured out but thanks

#

Avoiding mocking was the right move 👍

dense garnet
proud nebula
proud nebula
dense garnet
dense garnet
dense garnet
fallow meteor
#

can someone guide me simplest framework/library to itarate through xls file row by row and use these rows in data driven test website? im pretty much stuck right now as i cant figure out how to find element to fill text -> take row 1 from xls file -> send keys to form -> click action -> take another row of data from file -> fill form and click again action button, untill end of file

#

just need some good tut or guide, becouse there is so much ways to do it, and i need it simple, as im not a programmer at all actually

rapid patrol
#

seems like it should be simple enough with selenium and pandas?

look into reading in excel files with pandas, then selenium for interacting with the site

fallow meteor
#

everywhere i see solution to just read all the table contents, and im not sure how to implement that input method on website use rows 1 by 1, i manage to use pandas but my input tries to use all table contents at once

#

actually im not sure if i should use iteration inside fill form and click button function, or fill form and click inside iteration function

rapid patrol
#

why not iterate over the rows in the dataframe?

#
for _,row in df.iterrows():
  # do things with row variable
#

considering it's sequential, the fact that the iterrows method being slow shouldn't be a problem

fallow meteor
#

but will it serve one rowe to execute, and wait with next one for next call? dataframe is littebit confusing for me as tutorials shows specific usecases without reasonale explaination

rapid patrol
#

provided you write an asynchronous program, then yes

#

I imagine that's the problem you're facing, you're trying to run the df synchronously, where the web browser interaction is inherently asynchronous, you must wait between "attempts"

fallow meteor
#

so there is no way that i get varialbe named "input" with passed row 1 to it, then used on website element, then iterate through next row2, send it to same variable and us it on website, then go to row3 send it to variable make actions on website and so on?

#

ofcourse with element.click() between variable change

rapid patrol
#

yeah of course, you might want some sleeps, or await a response code being sent back as your determining factor for when to move onto the next row

fallow meteor
#

efficiency is not a thing, actually i have to use some sleep times to navigate website between searches as it is search box and i have to wait for results to be fully loaded on website

#

then provide new search quoery from list and repeat, but list is pretty long so embeding it in code is not possible, thats why im going with xls file

#

i started thinking that pandas is overengineered for that task and not straight forward for me to implement as im not exerience with programming coding beyond SQLqueries

#

maybe openpyxl would be a better option for this

lucid lily
#

Doing unit tests in django and this line causes to return this string for some reason, not really breaking bug but it annoys me for hours now

@classmethod
    def setUpTestData(self):
        user = User.objects.create(
            username="TestUser", password="VeryComplexFakePassword123[[]]")
        service = Service.objects.create(
            service_name="netflix", whole_service_name="Netflix", manager=user)
        group = PayingGroup.objects.create(
            group_name="My Netflix Group 1", monthly_cost="2", service=service, manager=user
        )
        subscriber = Subscriber.objects.create(
            name="Felix", iban="NL61RABO4110487447", debt="0", manager=user
        )
        subscriber.group.add(group)
        date = datetime.date(year=2023, month=3, day=29)
        #line under :/
        Payment.objects.create(
            subscriber=subscriber, date_of_payment=date, paid_amount="4", manager=user
        )
Found 4 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Felix
....
----------------------------------------------------------------------
Ran 4 tests in 0.013s

OK
Destroying test database for alias 'default'...

**Returns Felix ** on line 4 of output

lucid lily
# lucid lily Doing unit tests in django and this line causes to return this string for some r...

Here is Payment model object

class Payment(models.Model):
    subscriber = models.ForeignKey(
        Subscriber, on_delete=models.CASCADE, null=False)
    date_of_payment = models.DateField(
        auto_now_add=True)
    paid_amount = models.DecimalField(
        max_digits=6, decimal_places=2, default=0, null=False)
    manager = models.ForeignKey(
        to=User, on_delete=models.CASCADE, null=False)

    def __str__(self):
        return str(self.subscriber.name) + " " + str(self.paid_amount) + "€ " + str(self.date_of_payment)
proud nebula
proud nebula
lucid lily
#

yeah sorry for bad declaration

proud nebula
#

Can be annoying to find if you put a debugging print() somewhere and forgot about it for sure

lucid lily
#

I've tried to find it, but if I delete

Payment.objects.create(
            subscriber=subscriber, date_of_payment=date, paid_amount="4", manager=user
        )

problem goes away

proud nebula
#

That line triggers a LOT of code potentially

#

constructor of Payment, signals, just to say two obvious ones

lucid lily
#

now that you mention that I was tunnel visioned yeah I have signals connected to this model I will dig deeper thanks

proud nebula
#

Tunnel vision is the top reason to ask for help heh

lucid lily
proud nebula
lucid lily
#

xdd

proud nebula
#

hmm.. and not update total paid even

#

ouch

lucid lily
#

unfinished function

#

reworking app from admin only management to any logged in user can manage "dashboard" and some spiders and webs are left behind

pastel idol
#

hey
can anybody help me with pytest
how do i pass data from one test case to other
i have a function which traverses through a file directory and it generates the list
how do i pass that data to parmeterize marker

#

so i have 3 tests
test1 = it sends request and it gets a response
test2 = uses this response to test another parameter
test3 = uses the same response to test another parameter
how do i save the response
and send it to other testcases

ember maple
#

i

ember maple
proud nebula
pastel idol
pastel idol
ember maple
pastel idol
pastel idol
pastel idol
#

I’m new to pytest and testing 🥲

proud nebula
# pastel idol ??

Think about it. If you get the results of a function, and assert it's some value. Then you KNOW it's that value. So the other test can assume it:

def foo():
    return 5

def bar(f):
    return f + 4

def test_foo():
    assert foo() == 5

def test_bar()
    result_of_foo = 5
    assert bar(result_of_foo) == 9
pastel idol
#

So I can’t assume

#

It’s script that generates some values
I’ll have to test it

#

The output value keeps changing

#

So I assume I have 2 scripts
And both of these scripts are generating values
I’ll have compare these 2 scripts that they are generating the same value

ember maple
#

That sounds like your testing approach misfits

pastel idol
#

Test case 1 is to test if the first script executed successfully
Test case 2 is to test if the second script generated same output has first one

pastel idol
#

Thank you for the help! @ember maple @proud nebula

proud nebula
ember maple
pastel idol
pastel idol
proud nebula
# pastel idol why do you think so?

Because it makes reasoning about the code very difficult and implies you might have a mutability mud pile.

"Functional core - imperative shell" is a good way to live.

thorn leaf
#

Hi, I'm using unittest for the first time on a project. I've created a function that implements a discrete probability distribution, and I want to use scipy's chi squared test to demonstrate that the outcomes obey the expected distribution. I want to create a list of test inputs, and run them through a test_chi_squared method - how do I structure my code so that this method is called once for each test input in my list? (is this the right channel for this sort of question?)

maiden pawn
thorn leaf
#

is there a way to do it with unittest instead of pytest?

maiden pawn
thorn leaf
#

hmm ok

#

makes sense

#

currently im trying to wrangle my scipy.chisquared function

maiden pawn
#

there should be a way generating parametrized tests in unittest pithink

#

i remember being able to do that

#

but kind of no point to do that.

thorn leaf
#

if i need to install extra libraries probably not worth it

#

you know what im too tired for this i should just go to bed and tackle it tomorrow

#

thanks for the help, and goodnight

#

i might be asking similar questions tmrw

maiden pawn
#

but you should not go this way :/

#

just use pytest, what stops u from that

thorn leaf
#

i will switch to pytest

thorn leaf
#

aight i got parameterised testing working with pytest

pastel idol
#

Hey!!

#

I have a test case which collects data from 5 different machines using fabric
As this process is taking long time

#

I thought I'll use a seperate thread for each machine

#

I wrote a function which collects all the data
But function has few fixed values and one value which varies for different machine

#

I tried using pool
But it was throwing pickling error

#

How should I do it?

maiden pawn
pastel idol
#

I need the data to validate for each machine

maiden pawn
pastel idol
#

So in order to verify I need the data

maiden pawn
pastel idol
#

We are not using any database

maiden pawn
#

is this script forbidden to use databases for some reason? ^_^

#

for which reason

#

what stops you from using database to communicate data over networks

pastel idol
#

This is just a testing framework

#

So I'm using any database for now

#

The no machines is also less so I need to collect the data from machines itself

maiden pawn
pastel idol
maiden pawn
#

tempted to offer ansible-playbook. it is made to connect multiple machines in parallel, and getting files / configuring them

#

and it is made in python

#

but it is kind of too heavy overkill for this perhaps

pastel idol
#

Yeah I guess

#

But it looks interesting

maiden pawn
#

besides the size itself it would do the job pretty fine

#

already ready solution

pastel idol
#

But I am using fabric now

maiden pawn
#

ansible is way more developed solution exactly made for this goal ^_^

#

fabric is apperently solution that u need to adjust

#

if fabric is a sync library, then u need writing probably multithreading to do the job with it (or may be subprocesses)

#

thinking logically, fabrics actions aren't CPU heavy, which means mulththreading should be enough

#

all you need in theory launching many threads in parallel, and awaiting to collect result from them all

maiden pawn
# pastel idol I wrote a function which collects all the data But function has few fixed value...
from threading import Thread

def fetch_rates(base):
  response = requests.get(
  f"https://api.vatcomply.com/rates?base={base}"
  )
  response.raise_for_status()
  rates = response.json()["rates"]
  # note: same currency exchanges to itself 1:1
  rates[base] = 1.
  rates_line = ", ".join(
  [f"{rates[symbol]:7.03} {symbol}" for symbol in SYMBOLS]
  )
  print(f"1 {base} = {rates_line}")


def main():
  threads = []
  for base in BASES:
    thread = Thread(target=fetch_rates, args=[base])
    thread.start()
    threads.append(thread)

  while threads:
    threads.pop().join()
#

launch threads with arguments fpr specific machines, await, and get results

maiden pawn
#

(of course you can use thread pool too)

#

solution without thread pool is more simple, u can make it first ^_^ (check thread pool solution in the book)

pastel idol
#

@maiden pawn

#

Thank you!!

#

Multithreading is working

pastel idol
maiden pawn
pastel idol
#

Oh cool
Thank you!!!

pastel idol
maiden pawn
#

that is the most advantage of threads, memory is shared ^_^

#

if data structure is not safe, lock can be used to ensure that (list is probably thread safe, but lock can be added anyway)

#
from threading import Thread
import threading 
lock = threading.RLock()
results = []

def fetch_rates(base: str):
  with lock:
    results.append(base)

def main():
  BASES = [1,2,3]

  threads = []
  for base in BASES:
    thread = Thread(target=fetch_rates, args=[base])
    thread.start()
    threads.append(thread)

  while threads:
    threads.pop().join()

  print("finished")
  print(results)

if __name__=="__main__":
  main()
maiden pawn
pastel idol
#

how do i get it?

maiden pawn
#

i have no idea, google it ^_^ or read the book i gave you, there probably mentioned solution

maiden pawn
pastel idol
#

i found a copy online

#

but in chapter 6 there is some other topic

maiden pawn
pastel idol
#

is it by packt?

maiden pawn
pastel idol
#

thank you!!

#

i'll post the question there and see

proud nebula
#

that's horrible

maiden pawn
proud nebula
#

ugh, you can of course do 1 space indented.. that's the most evil shit

proud nebula
maiden pawn
#

during copy pasting all indents were removed. i was lazy to write 4