#unit-testing

1 messages · Page 4 of 1

proud nebula
#

wat. Don't you indent by hitting tab once to insert spaces?

maiden pawn
proud nebula
pastel idol
#

Hey! @proud nebula

proud nebula
weak marlin
#
class MongoDBWrapper:
    def __init__(self, host, port, db, collection):
        self.collection = motor.motor_asyncio.AsyncIOMotorClient(host, port)[db][collection]

    async def insert(self, documents, many = False) -> None:
        if many:
            await self.collection.insert_many(documents)

        else:
            await self.collection.insert_one(documents)

how to unittest insert? I dont understand how to mock self.collection and insert_* methods pithink

#

I use built-in unittest.mock

drifting sorrel
# weak marlin I use built-in `unittest.mock`

That’s a common problem with stateful classes. I would write a insert function outside of the class, that accepts a collection and the other inputs.

That one can easily be unit tested - you have control over the input and can stub/mock/monkey patch it.

#

Your class would then call the state-less function. And no need to unit test that part.

weak marlin
#

:(

#

but thanks!

drifting sorrel
#

If you don't like to extract code from the class, but still want to unit test, then you have to figure out a way to mock or monkey-patch motor.motor_asyncio.AsyncIOMotorClientthat is used in the init function of your class. The unit test will only verify if your many parameter is used correctly. The rest is only mocked out code. Are you sure this is a good fit for a test?

proud nebula
#

Yes.

drifting sorrel
#

It’s a good start to write tests for those. When lower level tests like util/helper tests overlap other tests that are on a higher level that cover the helper code: you can probably remove the lower level ones. I know that many fear removing tests, but that’s ok!

proud nebula
#

Lower level tests mean when they fail you have much a clearer understanding of what the problem is.

#

Plus it makes mutation testing much faster if you can make it use the lower level tests first or only.

drifting sorrel
#

I think it is a balance to find within the team, having unit tests that are “too low level” is at risk to slow down refactoring, since they define an implementation detail. From my experience it can be a good idea to unit test on a level up higher, but not too high up in the stack. Up there, you’ll very likely end up writing a lot of mocks. That’s painful. Too low level tests can be annoying too: you change one thing an a bunch of tests fail. Ideally, very few (at best one) test should fail.

#

During development, TDD is a good tool. But it is allowed to delete tests too. Nowadays I usually do REPL Driven Development, that is very much TDD-and-throw-away - sometimes too much of throwing away. It’s always a thing to have in mind, finding the balance (and ignore that coverage-metric).

proud nebula
#

Yea, that's a good point. If the lower level stuff is clean and reusable, then it's good to have unit tests. If they are just implementation details then maybe not so much.

bitter wadiBOT
#

:incoming_envelope: :ok_hand: applied timeout to @warm relic until <t:1681027127:f> (10 minutes) (reason: duplicates spam – sent 4 duplicate messages).

The <@&831776746206265384> have been alerted for review.

cloud parrot
#

Does anyone know good examples of open source projects with good structure of unit tests? When people use fakes and stubs instead of just mocks and separate integration/unit tests? Especially interested in database/broker interactions

drifting sorrel
old venture
#

guys, in my internship, the higher ups want me to use AI ML in testing APIs
one of the seniors suggested to use OpenAI models, if i can

#

is this possible?

#

I am newbie into the testing world, It would mean a lot if someone could give their views on this

#

API testing is a broad term to apply AI or ML,

#

so in what part of API testing can AI ML be applied and do you have any clue to that?

novel canopy
#

I've hired over a dozen interns over the last few years and this is, BY FAR, the most annoying thing in managing them.

#

90% are afraid to ask questions. Your mentors/bosses do not know what you know or don't know.

old venture
#

I did ask them to explain, the MD just told me some info abt supervised learning, neural networks and training dataset.

novel canopy
#

if you still don't understand, you need to ask them to rephrase

#

or be clearer about what you don't understand

#

etc, etc

#

I understand it's hard to get over the "I don't want to appear stupid" feeling. but you REALLY need to get over it. it's critically important. it's how you reach excellence

old venture
#

The MD said that he told what all he knew
My mentor, who is another person, has no idea on this as he works in web dev team

novel canopy
#

he did not understand your question

#

my advice is this: start from the end. i.e. ask him what the objective is, what the the final work product should look like/do. i.e. how do you know if you've been successful or not?

#

because, I suspect that is where you are confuzelled. amirite?

old venture
#

And then make requests and verify them

novel canopy
#

oh, ok. that seems clear enough

old venture
#

But that is very broad

#

How can AI model generate test cases

novel canopy
#

most can't. GPT-3.5 and GPT-4.0 can

old venture
novel canopy
#

well, you'd have to feed it the signature and docs

old venture
novel canopy
#

and perhaps provide some clarification

old venture
#

So for every API, the model has to be fine tuned?

#

Using docs

novel canopy
#

of course, how else would it know?

#

it's AI, not magic 🙂

old venture
#

That might be chaotic, cuz some API might have those cases possible but for some other APIs those cases might not hold

novel canopy
#

you're not really tuning the model here, the model is already set. you're feeding it inputs

#

but whatever

frosty perch
#

Hello 👋
With FastsApi I would need to override a dependency injected into the Path operation decorator. I found this https://github.com/tiangolo/fastapi/discussions/6479, but can't get the patch to work. anyone have any idea if there is an easier way to override this dependency?
Thx

GitHub

First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the FastAPI documentation, with the integrated search...

calm juniper
#

How to pass auth argument in responses.add while mocking, do you guys know any matcher
below is not working

        responses.post('https://pretialast/api/tcs/v1/user-auth/login',match[matchers.request_kwargs_matcher({'auth':requests.auth.HTTPBasicAuth(username="test", password="test@123")})],json=resp_dict,status=200)
proper wind
#

hi

#

how are you guys doin?

#

e

#

awhy no one is talkin?

#

or chatting?

#

due

#

thats weirds

royal orbit
# old venture I did ask this, and the MD guy said when an endpoint is given to ur Model, it sh...

I work on large language models, and this sounds like uninformed hype to me. Try https://github.com/schemathesis/schemathesis for auto-generated API tests.

GitHub

Guarantee flawless API functionality with thorough, high-quality test scenarios generated from your API specification. - GitHub - schemathesis/schemathesis: Guarantee flawless API functionality wit...

#

(disclosure: I also co-wrote the paper on Schemathesis)

hollow cobalt
#

I'm struggling to think of unit tests that would be useful for a sorting algorithm

#

I have one that tests whether or not it will give a properly sorted array but that's it

brazen needle
#

For this type of algorithm, testing cases that are "sparse" or "most extreme" is sometimes helpful. So I would suggest some inputs that are entirely sorted, except that two entries are swapped; possibly also an input that's entirely sorted except for one stretch somewhere; and also an input that's reverse sorted.

hollow cobalt
#

I see, that's very useful thanks!

dreamy arrow
dreamy arrow
#

I'm trying to "mock" a graphql server and it's responses within my unit tests -- the data is not important as I can easily just test the type/schema of the response and not the actual data inside but i'm trying to avoid spinning an external server as that would slow my testing down

royal orbit
#

No, it's a tool for testing APIs, which is pretty different from pretending to be the API. It does support generating GraphQL queries (etc) though!

patent osprey
#

Is it common practice to unit test terraform code? If so, which frameworks are best?

silent echo
#

Can someone tell me why my server is rejecting hashlib??

knotty lintel
#

Any e2e framework with support for AWS SDK can do that

knotty lintel
viscid basalt
dreamy arrow
proud nebula
chilly sentinel
viscid basalt
maiden pawn
#
leaden atlas
#

hi guys, can someone help me with this unit testing
There is a code(which is not allowed to change anything) and there is my solution to it, can you please check the possible mistakes that i made in the unit testing code
this is the code(with class Robot) - https://pastebin.com/pLcs1AeQ
this is unit testing code- https://pastebin.com/2nwFz9WU

maiden pawn
#

there is of course room to improve its further pithink but that is certainly beyond set task and requires preferably helper third party libs to be installed

proper wind
#

I have /tests in my root dir with test_etc.py, everything folder has an __init__.py.

#
user@pc:~/Desktop/r-nad/tests$ pytest
========================= test session starts =========================
platform linux -- Python 3.10.7, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/user/Desktop/r-nad/tests
collected 0 items / 1 error                                           

=============================== ERRORS ================================
__________________ ERROR collecting test_nashconv.py __________________
ImportError while importing test module '/home/user/Desktop/r-nad/tests/test_nashconv.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_nashconv.py:3: in <module>
    from ..environment.tree import Tree
E   ImportError: attempted relative import beyond top-level package
======================= short test summary info =======================
ERROR test_nashconv.py
!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!
========================== 1 error in 1.06s ===========================```
proud nebula
proper wind
#

no, I still get the exact same error

drifting sorrel
proper wind
#

ah i just fixed it. running as module worked but I had import .. still in there

proud nebula
#

You probably want something like from environment.tree import .... I don't like the package names environment, util and learn in the top level like that personally. It's a bit conflict-begging

proper wind
#

interesting. shame since I'd prefer to keep it very simple. Its not intended to be installed via pip or anything, I just prefer folders for organization vs just letting everything sit in root

proud nebula
#

You just need to get the imports right

rustic coyote
#

quick question, how would you build a unit test to test that a class quand find and read a specific config file in it's constructor?

rustic coyote
#

can*

#

sory lol

proud nebula
#

Ask one question at a time and people can help

gray imp
#

how can i fix this error

maiden pawn
crystal anvil
#

There should be a C:\Python311\Scripts folder where the executables are generated. That should be in your PATH, not site-packages

#

That or use py -m pytest

modern hedge
ionic jewel
#

@proper wind @proper wind

Do you guys know any Discord server that can help me?

My first account is not here becouse its sweden.
My first account[That Hi Guy] From sweden go baned by sweden cyber security for random reasons

My secound account [That Hi Gay] Made in Iran becouse i got deported to Afghanistan from sweden.

("I just want to ask")
[Can i join you guys to learn code?]
#I_AM_NOT_GOOD_AT_PROGRAMING!

I am from Afghanistan & My jobb is :
Nothing / Student

I can speak :
Persian , Dari , Tajik , English , Greek , Swedish , Danish & German.

My Hobby is :
Coding

My life is :
Trash

My favorit food is :
I ead evrything!

#

@proper wind Becouse i want to join & learn!

#

Nope!

#

Nope

#

I have girl firend!

#

I do!

#

I was in all of this country more than 2 years!

#

@proper wind سلام من میخوام به شما بپیوندم من اصلا گی هستم من میخوام کد یاد بگیرم !
کمک میکنی یا نه ؟

#

@proper wind Hallo das bist nich gut!

#

@proper wind 😦

proper wind
#

why not write it in Finglish

ionic jewel
#

Sorry

#

I will speak english!

proper wind
#

it's all BS

#

you don't speak Farsi

ionic jewel
ionic jewel
ionic jewel
#

HAHAH!

#

Kesafat!

#

@proper wind How to varify voice?

#

@proper wind How to open this button?

#

@celest pilot How to unmute my self?

weary quarry
dry portal
#

Hi,
can someone provide me some assistance with fixing my test_db?

I got this

@pytest.fixture(scope='session')
def test_app():
    app = create_app('tests.settings', validate_response=True)

    ctx = app.app_context()
    ctx.push()

    yield app

    ctx.pop()


@pytest.fixture(scope='session')
def test_db_schema(test_app):
    """A database for the tests."""
    db.app = test_app
    with test_app.app_context():
        db.create_all()

    yield db

    # Explicitly close DB connection
    db.session.close()
    db.drop_all()


@pytest.fixture(scope='session')
def _db(test_app):
    """Provide the transactional fixtures with access to the database via a Flask-SQLAlchemy database connection."""
    return db


@pytest.fixture(scope='function')
def test_db(test_db_schema, db_session):
    pass

When I try saving a batch of data created with factory-boy

    def save(self) -> M:
        db.session.add(self)
        try:
            db.session.commit()
        except IntegrityError as sql_exception:
            logger.exception(sql_exception)
            db.session.rollback()
            raise APIException(detail='Unable to save to DB') from sql_exception
        return self

I get error 'psycopg2.errors.UndefinedTable'

None of my models is being created to my test DB.

#

It's not an IntegrityError what I am trying to catch as I don't get my expected error message. As far as I can interpret the massive stack trace, it turns in to a FlushError at some point... Really clueless waht to do with this

ember maple
past echo
#

gm

pliant scarab
#

@chilly knoll You've sent the same message in several channels. Could I know what you're trying to do?

solid garden
#

Hi, is there a way to skip a pytest test based on a condition that involves a fixture?

river pilot
solid garden
#

And I think that using a fixture inside of a decorator won't work

river pilot
#

There's probably a way to have access to the fixture, but I don't know it off the top of my head. What does the fixture do? Can you link us to some code?

dire remnant
hexed cloak
# dire remnant Can anyone raise a Pull request with unit tests for this open source github repo...

The code currently isn't easy to unit test because the whole code executes an an import side effect. You should move all of that to functions using https://docs.python.org/3/library/__main__.html#name-main then unit tests can import individual functions and test them

marsh cedar
#

pytest question: if I have two "class" scoped autouse fixtures, but one is declared as a global function and the other is declared as a test class method, is there any ordering defined between them?

@pytest.fixture(scope="class")
def order():
    return []


@pytest.fixture(scope="class", autouse=True)
def fixture1(order):
    order.append('fixture1')


class TestFoo:
    @pytest.fixture(scope="class", autouse=True)
    def fixture2(self, order):
        order.append('fixture2')

    def test_order(self, order):
        assert order == ['fixture1', 'fixture2']

This test passes for me, is this order guaranteed?

#

the docs say that there isn't a defined ordering between autouse fixtures of the same scope, but scope here refers to the "scope" keyword, not class vs. global scope

proud nebula
#

TIL about fixtures on a class

#

Pretty evil code you've got there also.

marsh cedar
proud nebula
#

Mutating a fixture like that is pretty evil imo. Will make it really difficult to follow the code.

marsh cedar
#

according to the docs, that's a feature

#

idk though, I only learned how to use fixtures yesterday

proud nebula
#

I mean, mutation that isn't local is pretty evil itself. Imo you really don't want your tests to be hard to understand and debug. That will make your life miserable :P

dire remnant
dire remnant
proud nebula
proud nebula
#

Same reason we have patents actually. Publish in the open instead of keeping secrets.

proud nebula
dire remnant
dire remnant
fading nova
#

Hi. I'm trying to test an init function

#

Do I need to have a setup and tearDown methods

hexed cloak
hexed cloak
gusty siren
#

Hello, I come up with a general question, any advices or thoughts I have to have in mind to break software? (E.g push my own software to the limit)

maiden pawn
#

did he mean code architecture, or performance quality (of desktop part? of server part?), or testing for edge cases, or having good coverage or smth else

gusty siren
maiden pawn
#

that alone will make a difference already

gusty siren
#

And sometimes I have problems testings some scraping projects when they return me a Big json response.

Some scripts paginate over 30 documents in 500 pages and imagine that there is an error in the document 27 and page 259. (Some item fields not coming as expected)

How do I test that? Mocking the json?
I mean, save a sample response of the requests i make in json files and making unit tests on these responses?

maiden pawn
# gusty siren And sometimes I have problems testings some scraping projects when they return m...

static typing with structs again the answer. (using library pydantic)

class ParsedData(BaseModel):
  field1: str
  field2: int
  nested_objects: List[BaseModels]

parse data to structs. use them in your code with mocking

Have integration tests checking that JSON files downloaded from real stuff are still fitting the structs.
Run unit tests quickly during development
Run integration tests at least with CI or smth. mocking them or not, depends on how easy to obtain JSON all the time for you, how much it is fragile. it can be mocked too. but it will break only tests that transform JSON to BaseModel

gusty siren
maiden pawn
bitter wadiBOT
#

5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.

maiden pawn
maiden pawn
gusty siren
dire remnant
livid bay
#

Hey, so i have a tool that has 8 textfields. I can give the option to open a PDF file per dialog and it will extract stuff from the PDF following a regex schema. So far it is working with 2 files, but i want to write tests to make it work with even more PDFs without me testing on each one independently.

The strings that are extracted from the PDF will be written into a list containing tuples of strings and integers. My approach would be to somehow load the pdfs after another, create multiple lists with the results available and compare the extracted lists with the pre defined ones, but i'm still clueless on how to approach. Screenshot shows how it looks inside the tool.

The method on how the list is filled is:

path = self.open_file_dialog()
formatted_krits = auto_pdf.krit_list(path)
i = 0
for x in formatted_krits:
    self.krit[i].setText(x[0])  # list that i want to test
    self.krit_von[i].setText(x[1])
    i += 1
rugged yarrow
#

👋

#

I wanna know a bit about unit testing. What is it used for and how can it help me? And, where can I learn unit testing?

maiden pawn
#

learn after that more practical head on approach in TDD by Kent beck

rugged yarrow
#

Ok thanks

silk ravine
maiden pawn
# silk ravine Guys how can I test my python library https://GitHub.com/NaviTheCoderboi/NewsDas...

classic problem and solutions

  1. creating a Spy of NewDash client, that reads maximum raw files of html bodies/json bodies received from real NewDash.
  2. Your other code is tested to interact with this Spy as expected, parsing data, rendering results, asserting expected stuff to see.
    This was unit testing section

in addition having few pytest tests, marked as @pytest.mark.regenerating?
by default aren't runnable. though in CI probably should at some point
They will download new version of JSON bodies/HTML returns from NewDash, that will be caught by your NewDash spy for the rest of tests

#

====
in this way during local development => you have fast predictable unit tests that are fully local and aren't needing to interact with NewDash. fast and easy to debug.

#

if u have CI running that runs both Unit tests and Integration(first regenerating then running your unit tests, which can be done in same pytest fixture that was reading saved JSONs before)
You will have running integration tests to validate code being valid too

#

optionally for full solution, since your solution involves parsing and main meaning of it => CI integration tests should be launched as CRON job to run repeatedly, once in a while checking your code as still being valid

maiden pawn
maiden pawn
# maiden pawn classic problem and solutions 1) creating a Spy of NewDash client, that reads ma...

Last, but not least, make sure when your Spy yields parsed data to other unit tests, it is maximum type hint enforced one by mypy and data transfer objects (DTO), which is nice to be Pydantic BaseModels, are using all the time maximum... precise values (No dictionaries!!). Only nested Pydantic BaseModels, and simple types like strings, integers, floats, datetime. Even those ones should be further typed by from typing import NewType, to decrease confusion spreaded by having too many values of same simple type)

#

then your other tests will not depend on changable / dynamic / unknown reliably implementation details of NewDash

#

but it will depend on your own fully controlled and secured conversion of NewDash data into your own application code data

#

xD that is how your unit testing NewDashSpy should be looking. the wall, that allows no untyped third party data structures into the rest of your city(code), until it was fully converted into legit citizen of a city

#

if for some reason your requirements wish to be not using Pydantic, so that users of library would not be forced to use it... ergh, u could avoid its usage by using regular python Dataclasses, but i would not recommend it.
Pydantic solves a lot of problems out of a box, including OpenAPI data generation and serialization and deserialization and etc. Library advantages of it, too huge to be not adding.

#

mypy / pytest are libs that aren't needed distributed to user since they are for your dev env / CI env / testing env only

azure tusk
#

I had a discussion about unit testing in #career-advice . I just don't see how I can unit test this if it's rendering exactly how I want to.

import React, { useState } from 'react';
import axios from "axios";
import './App.css';



function App() {

  // new line start
  const [profileData, setProfileData] = useState(null)

  function getData() {

    axios({
      method: "GET",
      url: "/profile",
    }).then((response) => {
      const res = response.data
      
      setProfileData(({
        profiles: res.profiles
      }))

     

    }).catch((error) => {
      if (error.response) {
        console.log(error.reponse)
        console.log(error.response.status)
        console.log(error.response.headers)
      }
    })


  }
  
  function profileList() {
    const pList = profileData.profiles.map(( profile, i)  => 
      <p>
      <ul key={i}>
        <li>{profile.name}</li>
        <li>{profile.about}</li>
        <li><img src={profile.image} width="250px" height="250px" alt={profile.name}></img></li>
      </ul>
      </p>
    
      );


      return <ul>{pList}</ul>
  }
  
  console.log(profileData)

  return (
    <div className="App">
      <header className="App-header">
        <p>To get the profile details: </p><button onClick={getData}>Click me</button>
        {profileData && <div>
             {profileList()}
            </div>
        }                            
      </header>
    </div>
  );
}

export default App;
rigid ridge
#

This is a dumb test isn't it, because the system running this test can only test one of these cases?

class TestCheckOutput:

    def test_not_windows(self):
        if platform.system() != 'Windows':
            output = check_output(['ls'])
            assert output != ''

    def test_windows(self):
        if platform.system() == 'Windows':
            output = check_output(['dir'])
            assert output != ''


def check_output(cmd: List[str]):
    return subprocess.check_output(cmd).decode().strip()
        
rigid ridge
#

testing is too hard. My tests pass alone but not together.

#

This is why I never write tests because if I did my entire life would be writing tests.

#

and nothing would ever happen

brazen palm
#

you could change those 2 tests into 1, and conditionally use either ls or dir

#

depending on the system

#

tho I'm not sure what the point of those tests are

rigid ridge
#

check_output is used to read the output of a subprocess and it needs to work. lol

#

or like....

#

is that a situation where I'm not even testing my own code, I'm running a test on a wrapper for subprocess.check_output()

#

yeah okay there is no point for that test I guess.

#

The bigger problem I have is that much more complicated tests are breaking each other but pass alone

#

I thought thewhole point of using the testing framework was to run the tests in isolation

#

honestly, these tests are a waste of my time lol. I'm just deleting them.

brazen palm
#

yea I thought they seemed excessive since the code you're testing looks like a really thin wrapper around a stdlib method

#

and I guess it depends on what you're testing that is causing the tests to interfere with one another

#

might need a pytest plugin to mock some things possibly

rigid ridge
#

yeah... I took a break and Im less frusterated. I think I am trying to learn testing by writing tests for things that are too complicated for my understanding of the library and testing in general. I've decided to focus on less complicated things but things that still are useful to test.

#

I decided to eliminate that wrapper function. I think when I started writing the function I didn't expect it to be such a simple thing.

maiden pawn
maiden pawn
# azure tusk I had a discussion about unit testing in <#470889390588035082> . I just don't se...

shrugs. so in conclusion you can test your code in next strategies

  1. You receive expected data from backend (highly preferably doing it with typescript in addition) (this is kind of integration test, checking that your backend depdency is behaving in expected way)
  2. you can make test that checks, if page is rendered -> and some simple phrase is present (very simple test)
  3. you can make test that interacts with React testing API, loads this component, clicks the button, and asserts that expected data is present (This can be made pure unit test, if you will mock here data from backend (again better doing it with typescript, otherwise does not make sense a lot)

test #2 can be erased when you made test #3, since it repeats test #3. well, or u can just extend test #2 into test #3

maiden pawn
rigid ridge
#

I see.

maiden pawn
# rigid ridge This is why I never write tests because if I did my entire life would be writing...

ergh, just learn typescript. you are frontender, it will be already big improvement of your code quality and life. (type script kind of replaces half of tests for you)
Unit testing is harder to get in frontend due to your environment being way more heavily polluted with framework than for other devs. (Frontend frameworks enforce their own hard to unit test code logic)
You probably need more intensive backend experience first, before u will get how to test frontend code

rigid ridge
#

um, I'm not testing frontend code. It's not a web application at all actually.

maiden pawn
#

oh, sorry, mistook you on second rereading after some time with that guy above you

rigid ridge
#

ohhh it happens

maiden pawn
#

skip my last msg about frontend

rigid ridge
#

I have another question related to pytest...

maiden pawn
rigid ridge
#
class LocateRetroarchConfigDir:
    
    # Passes
    def test_found_from_nonexisting_path_in_config(self):
        config_dir = locate_retroarch_config_dir('')
        assert config_dir == "dsfdsfdfs"

# Fails
def test_found_from_nonexisting_path_in_config():
    config_dir = locate_retroarch_config_dir('')
    assert config_dir == "dsfdsfddsfds"

Both of these should fail. Why?

maiden pawn
rigid ridge
#

the question related to the fact that they are the exact same test, one is a method of a class and one is a function. Additional context for that problem shouldnt be needed. Theyre the same test.

maiden pawn
rigid ridge
#

the only thing i can think to do is just go back to using functions because the classes are giving me passes even when i intentionally make them fail

maiden pawn
#

sure, they should be receiving same output, both should fail or pass

#

if it is not happening => u are having some kind of bug i guess ^_^

#

when i grouped tests into classes for pytest, it worked fine for me

#

nothing changed

rigid ridge
#

yeah I dont really understand because it seems to work in other classes. but it's making me not trust the results.

#

look

#
class LocateRetroarchConfigDir:

    def true_is_false(self):
        assert True == False
$ pytest tests/
=============================================== test session starts ================================================
platform linux -- Python 3.10.6, pytest-7.3.1, pluggy-1.0.0
rootdir: /mnt/hdd/hacks/smwc-scraper
collected 6 items                                                                                                  

tests/test_utils.py ......                                                                                   [100%]

================================================ 6 passed in 0.05s =================================================
maiden pawn
maiden pawn
rigid ridge
#

ohhhh

#

damn

maiden pawn
#

if i remember correctly Pytest uses Unittest convention for it
TestXXXXX is expected name

rigid ridge
#

yeah im dumb

#

thanks

azure tusk
# maiden pawn shrugs. so in conclusion you can test your code in next strategies 1) You receiv...

Just revisited one of my old projects. This is the back-end code for it that contains JSON data.

from flask import Flask, url_for

api = Flask(__name__)

@api.route('/profile')
def my_profile():
    response_body = {
        "profiles": [{
        {
        "id" : "4",
        "name": "Madara Uchiha",
        "about": "was the legendary leader of the Uchiha Clan. He founded Konohagakure alongside his childhood friend and rival, Hashirama Senju, with the intention of bringing about an era of peace. When the two couldn't agree on how to achieve that peace, they fought for control of the village, a conflict which ended in Madara's death. Madara.",
        "image": "/static/madara.jpeg",
        


        }]
    }

    return response_body

with api.test_request_context():
    print(url_for('my_profile'))


I actually wrote a test case for this. How is this for testing?

rigid ridge
#

how do you mock a binary file that when executed with binaryfile --version will output th1s 15 4 t35t. I could create a binary file, write some bytes to it, and then pass it to subprocess.check_output.... but it would just give me an OSError because it wouldn't be a real executable.

#
def get_bin(path: str, 
            which_cmd_name: str, 
            version_output_substrings: List[str]) -> Path:
    
    def run_which() -> Path:
        if platform.system() != 'Windows':
            try:
                path = subprocess.check_output(
                    ['which', which_cmd_name]
                ).decode().strip()
                return Path(path)
            except (subprocess.CalledProcessError,
                    PermissionError) as e:
                print(f"{which_cmd_name} is not installed")
                return ''

    if path:
        bin_path = Path(path)
    else:
        return run_which()
    
    if bin_path.exists():
        try:
            # any(substring in string for substring in substrings)
            version_output = subprocess.check_output(
                [bin_path, '--version']
            ).decode().strip().lower()
            if any(substr in version_output for substr in version_output_substrings):
                print("Path in config.py is valid")
                return bin_path
            else:
                print(f"This is something other than {which_cmd_name}")
        except (subprocess.CalledProcessError,
                PermissionError) as e:
            return run_which()
    else:
        return run_which()

This is what I want to test.

you use it like this

flips_bin = get_bin(
        FLIPS_BIN,              # This is a user supplied path to a binary that may or may not be valid
        which_cmd_name='flips',   # This is the command where the binary can typically be found if installed to PATH
        version_output_substrings=['floating', 'flips']   # These are substrings found in the output of `flips --version` that confirm its flips.
    )

I have no idea where to begin testing

rigid ridge
#

this is the best I have


class TestGetBin:

    def test_found_from_blank_path_in_config(self):
        bin_path = get_bin('', 'bash', ['GNU bash'])
        assert bin_path.exists()

    def test_found_from_nonexisting_path_in_config(self):
        bin_path = get_bin('/does/not/exist', 'bash', ['GNU bash'])
        assert bin_path.exists()

    def test_found_from_existing_path_in_config(self):
        bin_path = get_bin('/bin/bash', 'bash', ['bash'])
        assert bin_path.exists() and str(bin_path) == '/bin/bash'


maiden pawn
#

plus using dictionaries of data is kind of bad too. use Structs of pydantic base models ^_^

azure tusk
maiden pawn
azure tusk
maiden pawn
#

usually working application launching and test launch are different entrypoints of applications

maiden pawn
azure tusk
rigid ridge
#

I have a really crazy idea. I was thinking about... I don't really want to send unnecessary requests to test my scraper, so instead I saved a copy of the html that the server sends to github, and in the test, and I use that instead of the real server. You could argue that if what server sends is different then the tests cant be trusted but... in reality that is the eventuality of any scraping tool anyway. So doing it this way just reduced the requests used to test.

kind meadow
#

Testing a scraper is an interesting situation, but generally what you did is for the best. If your test relies on an external server, it can go down unexpectedly which means intermittent failures for your tests

azure tusk
maiden pawn
maiden pawn
maiden pawn
# azure tusk Can you provide me a resource on how to write test cases for back-end code?

Basic flask examples with pytest
https://flask.palletsprojects.com/en/2.3.x/testing/

If u will start using sqlalchemy, see here to simplify tests
https://factoryboy.readthedocs.io/en/stable/orms.html

Otherwise read how to use pytest
https://docs.pytest.org/en/7.1.x/contents.html

But how to test from theoretical and practical approach?
Theory will answer book
Best principles and practices by Khorikov

How to write unit tests from practical theoretical approach, answers book
TDD by Kent Beck

azure tusk
#

is my_project the name of a folder or file?

maiden pawn
maiden pawn
#

it includes you to learn preferably, how to organize python files/modules into folders/modules/packages, without doing PYTHONPATH hacks.

maiden pawn
#

at some point you will still need learning such basics

#

and it is possible to mess up Django greatly beyond salvation too, if u make too mad code

azure tusk
maiden pawn
#

~/ lets asssume is an address of your root folder of a projects

#

~/my_project.py file can be containing code that you can import as from my_project import create_app
assuming you will have python launching the code from same level?
so, python is allowed launching your code in this way to work correctly python3 my_project, python3 my_project.main, python3 -m my_project, gunicorn my_project.main
as long as u launching code in this ways, python will discover automatically absolute imported files and folders of code from ~/ root of your project. (Because you launch with absolute path starting from ~/ root of your project)

#

your code can be located in
~/app/my_project.py and it can be imported as from app.my_project import create_app, and possible to launch as python3 -m app, or python3 -m app.my_project or gunicorn app.wsgi, it will work both fine then

#

ergh, it is about getting how python importing works

#

your files like my_project.py can be put into folders of code like app

#

as long as app folder has __init__.py at least as empty file, it can be having additional code of files like my_project.py

#

um, probably not very understandably explained, read somewhere how python absolue/relative importing and packaging works, that mentions all the things i mentioned

maiden pawn
azure tusk
#

Just to simplify things, You said to run test cases that a py file has to be separate from the file that I am trying to test right? So somehow the test.py code has to access the base.py file somehow in order to test the code in base.py. That's comes with importing. So I would have to write something like

import pytest from base

right?

maiden pawn
# azure tusk Just to simplify things, You said to run test cases that a py file has to be sep...

yes, it is assumed you have for example base.py as your working code
and all test files are explicitely tests.py or test_somename.py... at least according to generic rules of how python programms are usually structured

your test in tests.py wil lbe importing from base.py right and having written smth like

from base import create_app

def test_abc_route_works():
  app = create_app()
  resp = app.get("/abc") 
  assert "123" in resp.content
maiden pawn
maiden pawn
azure tusk
maiden pawn
azure tusk
# maiden pawn no, this will not work. re read flask documentation https://flask.palletsprojec...

I believe that my base.py code does not have an app creating function so it's a little confusing.

from flask import Flask, url_for

api = Flask(__name__)

@api.route('/profile')
def my_profile():
    response_body = {
        "profiles": [{
        {
        "id" : "4",
        "name": "Madara Uchiha",
        "about": "was the legendary leader of the Uchiha Clan. He founded Konohagakure alongside his childhood friend and rival, Hashirama Senju, with the intention of bringing about an era of peace. When the two couldn't agree on how to achieve that peace, they fought for control of the village, a conflict which ended in Madara's death. Madara.",
        "image": "/static/madara.jpeg",
        


        }]
    }

    return response_body

with api.test_request_context():
    print(url_for('my_profile'))

#

My back-end is proxied in package.json so that's how I get the back-end running

{
  "name": "fullstack-flask-react",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://127.0.0.1:5000/",
#

So the example in

flaskr/__init__.py
import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

Isn't what I use.

hexed cloak
maiden pawn
#

first time python has its own challenges

azure tusk
buoyant oracle
#

Instead you can tweak the code it generates to get the output you want

proud nebula
#

Or they are just copy paste from some test that was part of the training set 🤣

fervent blaze
#

Hi, everyone! I'm getting hired for a QA position, and I'll be using Python. Do you guys have any suggestion as to what I could be learning to "hit the ground running"?

I've never really done testing with Python. I've been looking into Selenium so I can learn it a bit (Selenium will be used on the job).

Perhaps any Python modules that help with testing? Thank you in advance!

earnest sequoia
#

pytest is the most commonly used testing framework in Python, so you should look into that. (It is compatible with unittest which is part of the standard library.)

#

!pip pytest

bitter wadiBOT
fervent blaze
earnest sequoia
#

I believe you can write Selenium tests on top of that

#

Personally I also use hypothesis for property-based testing, but that may be more suitable for directly testing frontend/backend code rather than browser-based testing

fervent blaze
earnest sequoia
#

yeah, you should take a look into that library

#

it makes you think differently about how to structure your tests

proud nebula
tight pond
restive bay
#

how do you make unit test for scripts where it needs sudo commands and opens system files that are root?

ember maple
restive bay
#

I tried pyfakefs and it works mostly for non-system files

ember maple
#

for example - the terms unit/integration/e2e are a bit washed up these days, but one can meke some pretty clear cases between "functions that do stuff without those perky side-effects" vs "functions that interact with the world" vs "taking all of the world for a spin" (+- some logging ;P)

restive bay
#

wow cool terms

maiden pawn
#

in strict strongest case, yes, it would be impossible to have it unit test

#

well... in some more light definition, we can still make it as local unit test (bordering integration testing)
if we use docker containers to test this functionality
whole functionality can be easily tested within same CI pipeline without external resources

ember maple
#

if it spanns processes, its not a unit, in any way even close resembling the initial definitions

restive bay
#

hey you have more articles or books I can read about this?

ember maple
#

and redefinging everything until it means anything is just dishonest

maiden pawn
#

anyway => with CI and containers => and u still own fully the environment / application / infra related part of code for it, it is still unit testing to me 😛 or at least unit testish integration.

#

at least we can agree hopefully, it remains local testing without uncontrolled remote dependecies 😛

restive bay
#

still.. files can just be strings returned by functions right?

#

files/folders whatever their owners are

maiden pawn
#

lets use the termin... at least it is locally testable functionality ^_^ in a way that test runs can be isolated in a good way from each other. and that's what is important. (repeated test runs do not pollute test results of next test runs)

ember maple
restive bay
#

hmm right, I guess when the command starts with sudo python -m unittest

ember maple
maiden pawn
#

btw, why would you need sudo to test your functionality in the first place

restive bay
maiden pawn
#

what stops you from running tests as python3 -m unittest ^_^

maiden pawn
ember maple
maiden pawn
restive bay
#

ok gotcha intensely not using sudo for sure

#

for context the system files are log files

ember maple
#

its like - there are cases - but so far in all of those cases the dev is having a strong drink and asking themselfes why they couldn't avoid that

ember maple
restive bay
#

that's not unit testing though?

ember maple
#

giving root for log file reading is pretty much a malpractice - use groups and prermissions no need for root ever

restive bay
#

agreed. though that's out of my scope

hollow galleon
#

maybe im just a fucking dumbass, but testing seems completely unnecessary to me.
the concept of having so many files just to test your fucking program when you could just enter in the values yourself seems ludicrous. and at that point, why even do the testing at all? you know what values will fail

uneven beacon
#

though frankly im terrible at writing tests, ive got a couple packages that id like better coverage but i dont know how to test them due to their I/O and side effects (or maybe im too lazy to figure it out)

stuck lynx
#

at what level of complexity do you see it as a reasonably persuit to start adapting a code base for testing, the overhead in the codebase always felt rough to me vs having some external test program that called on the functions and tested against which left the source effectively clean?

or do I have things flipped and that is a valid use case?

hollow galleon
uneven beacon
brazen needle
ember maple
# hollow galleon maybe im just a fucking dumbass, but testing seems completely unnecessary to me....

also note that there is a difference between a random one off one can code together in half a hour and never change and something that is large, multi months effort customer facing, changed over years by different people of different skills

it can seem low value to add tests to the immutable one off
its typically always saving ones ass to add tests to the longer term thing that will change by many

also a fun caveat, the one-off can often accidentally turn into something permanent that will change over time, and its typically very hard to test something thats throw away and anti repair ^^

so adding tests and striving to make things easy to test tends to have delay payouts and wins

restive bay
#

Unit testing is not even near comparable to software testing

#

@hollow galleon the keyword to focus on is 'unit' the smallest possible code that you can test

proud nebula
buoyant oracle
#

Unit testing something people do to see if something works. As simple as that

drifting sorrel
#

Continuing: TDD is a workflow for unit testing. A nice side effect of it is tested software, but the workflow is more about designing code in a step-by-step approach.

hexed cloak
#

Sort of makes sense to change the title of this room to software testing

tight pond
#

Ok so totally new at TDD and I've backed myself into a corner. So say I need to got to an ftp server and get a csv file. I'm not ready to code out a mock for the ftp module I'm importing because it's pysftp and well maintained, so I test for the presence of the file in my cwd. I've now created the function to grab the file, but unless I run the code (or move a file over) I can't see how I get my assertion to come true. What am I missing?

restive bay
#

I would wrap the ftp request code in a function and I would mock that function returning success and failed responses

tight pond
#

Now to read up on mocking. I appreciate the advice, thanks!

maiden pawn
tight pond
smoky gorge
#

Hey all, happy monday.

Curious about testing this scenario:

I have code that returns a JWT from our Azure AD instance using the msal library. It gets this JWT by:

  1. instantiating an object of msal.ConfidentialClientApplication and passing 3 args to the constructor: a client ID (string), our Azure AD instance endpoint (string), and our Azure AD client credential (string).

  2. calling a method of this object (acquire_token_silent) and passing in the 'scope' of the Azure AD instance (string) and the account (None).

  3. if 2) fails, call another method (acquire_token_for_client) and pass in only the scope.

  4. return the JWT that is in the response from Azure AD.

As you can see, the main functionality is from the msal library, all I do is give it the various string arguments which are environment variables that are gotten by os.getenv.

What is the proper approach to testing this? I essentially didn't write any code, just making calls to methods that other people wrote. Is this something I shouldn't unit-test? Grateful for any suggestions.

smoky gorge
fervent blaze
#

Would you folks have any suggestions for what IDE to use if I'll be doing QA with Python? I've been using vscode for a few years now, but I've seen some QA developers using PyCharm. I'm wondering if there is any reason to use one over the other?

This question is tied to the fact that I am learning version control, and will be tasked with choosing what editor I'll be using.

Thanks in advance. 😄

late cypress
#

Anyone have a good recommendation for a 'why use Pytest' article? Trying to push my team to cutover to using it but a lot of folks are still using Unittest for everything

novel canopy
#

the output is prettier

#

it's extendable

#

that's all I got

late cypress
#

Yeah, just trying to see if anyone has a good source I can throw at them; why make my own arguments when someone else already wrote a better one 😄

#

IMO fixtures/etc are a powerful enough reason to do it

drifting sorrel
#

Less code with pytest, not to be underestimated.

late cypress
daring coral
#

Hello all, fairly new to working with pytest. Running into an issue where all tests report passing but the pytest process is hanging and not letting the test run complete. running with --setup-show it reports that the session scoped fixture is calling the teardown. This fixture is launching a mock api server that the projecct I am testing is calling endpoint from. Any thoughts on how to troubleshoot the hanging / orphaned pytest.exe process I am seeing?

hexed cloak
restive bay
restive bay
#

Hmm, you might be right. I haven't tried any coverage tool outside pycharm though

proud nebula
#

I have. And PyCharm will literally ask you before if you want to install coverage.py into the python environment if you try to run coverage the first time on a project.

harsh warren
#

If I want to do test of functions that do api calls should I create variables of different responses and use Mock to make the call return per Test. Or is there a better way to test around api, I do not want to spend more on the api calls than I already am.

rough cloud
#

idk if this is related to unit testing

#

however is python a good language to run physics experiments

#

ping me if you can help me plsssss

drifting sorrel
random sorrel
#

hey, quick question, let's say I have functions foo and bar and foo calls bar.

If I write a test that only calls foo, and calls bar implicitly, coverage will still show bar as covered. correct?

Is there a tool that can tell me which functions have tests just for them?

proud nebula
proud nebula
#

This entire thing is a problem for mutation testing...

drifting sorrel
#

The term mutation testing was new to me, but in general I don’t think it is a problem when a unit test will indirectly “test” underlying functions. Otherwise you would need to write a corresponding test for each function, and that sounds a bit strange to me since the implementation behind the code shouldn’t be that much relevant (except for monkey patching I/O).

#

In short: it’s a good thing aiming for as few unit tests as possible.

#

But mutation testing is probably a different use case 😀

random sorrel
#

So the problem that the main functions works and technically "things are covered" isn't all that helpful. I can think of some specific cases where I want functions to do very specific transformations. But I have to cross my t's that I'm actually doing that.

#

Also, the most recent thing I did with that particular code was rewriting parts to make it compatible with multiprocessing and at that point I only have "???", "timeout" and "something that the code doesn't fail over, but it doesn't look right either".

drifting sorrel
#

That sounds more like an integration test?

While refactoring, adding unit tests to the extracted code would sound reasonable to me and using TDD would be even better!

What I meant with as few unit tests as possible, is to find a balance between testing one thing, but not necessarily test every single part of a feature.

random sorrel
#

Yep, and I agree, I just know I have too few 🙂

proud nebula
# drifting sorrel The term mutation testing was new to me, but in general I don’t think it is a pr...

There's some problems with mutation testing that is coverage guided where it will run too many tests, slowing everything down. Think of it like this: if you have a strip_prefix function in your code base, you don't want to rerun tests for everything that uses that function if you are working on it. You want to make sure you have 100% mutation coverage for that function itself and only run the tests specifically for that function when working on it. Otherwise the complexity grows exponentially.

#

I'm the author of mutmut btw, so I care a bit about this subject :P

dense garnet
#

Surely there has to be a better way to vary over n parameters than an nx nested for loop?

tribal gulch
#

Hi, what is the best way to debug a unit test?

#

All my test fail on one branch fail but when I change to the dev branch for example they all pass for some weird reason

tribal gulch
#

Fixed my issue

drifting sorrel
maiden pawn
maiden pawn
proud nebula
tribal gulch
#

Yeah I used pycharm as IDE but will keep that in mind.

serene basalt
#

I am writing documentation for a package that I am creating and I need to put code examples within the SDK in many places, what is the best way of doing this so it is DRY, automated, and tested easily?

To write code documentation I am basically grabbing code examples from the tests directory and putting them in the docstrings. However, this is a problem because every time the tests update then the documentation would also need to be updated as well, which could create a lot of work. Is there a better way of doing this?

#

feel free to @ me

maiden pawn
serene basalt
#

material mkdocs

maiden pawn
#

anyway => sphinx has features to embed file contents into docstrings

#

possible at least with Google style docstrings or some default sphinx syntax

maiden pawn
serene basalt
#

the only thing that I found for mkdocs was code include plugin, but it seems like it is unpopular because it only has 11 stars, and makes me a bit anxious about using it

maiden pawn
serene basalt
#

I think this is for restructured text, but I am using markdown

#

is there any way to do it with markdown because the whole project is already written in markdown?

serene basalt
maiden pawn
serene basalt
#

hmmm, it seems like there is no easy way maybe

#

should I just use the mkdocs code include plugin?

proud nebula
serene basalt
proud nebula
#

Those aren't screenshots, they're iframes of the actual html that the view with produce too. WAY better than screenshots for so many reasons.

proud nebula
# serene basalt is it open source package by any chance?

weeell.. it's open source in that it's in the iommi code base. I haven't broken it out to a separate package though as I still want the flexibility of changing it all without having to make a new release of some other package.

regal perch
serene basalt
regal perch
#

yeah but aside from the technical difficulty: if that happens often, does that mean your library upgrade will break all client code because it's too often breaking backward compatibility?

#

imo, even if your problem of auto-generating things is legit, you also might have an issue that you seem to struggle with a highly frequently changing specification. and for a client that reads your doc, if every interface changes from one release to another, it can become extremely frustrating

#

(pandas for example, rarely breaks its example set: previous examples are likely to still be good examples from one release to another; you might have new examples, but "breaking" examples seem to be more rare). if you make that kind of doc, I assume you target some customers. so maybe it's interesting to think about what kind of robustness you can guarantee for them

spring niche
#

hi is there a good reference to learn pytest ?

maiden pawn
proper wind
#

Hello! Is there a methodogy to test a function with all possible outcomes other than simply writing a test for each possible case?
lets say I have the following example and someone uses a string and a dictionary as arguments.

def test(num1,num2):
  return num1+num2

I am trying to avoid try except solutions because in most cases it's simply sweeping the problem under the rug

should I write each specific such case on hand? Is there a "simple" way to make unit testing for all possible arguments?

maiden pawn
#
def test(num1: int,num2: int) -> int:
  return num1+num2
#

you type acceptable type values of an object

#

and Mypy enforces that no code parts invoke this function with incorrect value types

maiden pawn
#

as helpful friend, good to use pydantic base models when you can (for data transfers)
anything that gets into them, has automated ensurance that on output they will have correct type

#

but as some point typing in python has its limits

#

and it is easier will be... just switching to another language at some point

#

I learned golang and learning java for those purposes.

maiden pawn
# proper wind Hello! Is there a methodogy to test a function with all possible outcomes other ...

there is python substition btw for those purposes. using libraries with fakery data generation, using libraries like schemathesis that will check your OpenAPI typing declaration of web endpoints and will try all possible data generated and inputed into them
but those are all half measures again, making many extra moves for one simple idea => easier to have static typed language than language with many many crutches to substitute it

proper wind
maiden pawn
maiden pawn
#

transferable experience between languages

proper wind
#

Every single thing you wrote gave me great insight. I will read through all of the modules. I learned about pydantic through fastAPI but I didn't make use out of it outside of fastAPI. I am working with people that care little on testing. some example is crawling information and their code is full of try excepts with no logging. Yes the problem I am facing is steps before unit testing but will help me put some things in order

maiden pawn
# proper wind Every single thing you wrote gave me great insight. I will read through all of t...

pydantic base models are perfect to be used... as data transfer objects across code.
You know.. it is better at some point using class of data instead of dictionaries?
but classes are sometimes needed quite simple, just for data, that is where python dataclasses came in https://docs.python.org/3/library/dataclasses.html, as they boilerplate constructor of class, hashing and etc
pydantic base models are even cooler, since they ensure correct type of values on output.
Quite safe and good data transfers of data objects across your python code. At least some typing safe place in a quite important place.

#

with having good self documentation to all possible present data (obviously way better than just using dictionaries that could be having anything inside entered at any place and deleted at any place)

#

plus they give you for free boilerplated serializers and deserializers into dict and json! (and even openapi schema)

#
class Point(BaseModel):
  x: int
  y: int
#

and that's it. you don't need to test if they will have correct types any longer. this is ensured at runtime automatically

#

runtime type validation

proper wind
maiden pawn
#

you can rewrite it in a better way... with reusability through using generics
once inserted type will propagate to others like automatically switching that all expected types are int or double and etc

proper wind
#

yes you did. I am asking if the outcome/response of someone calling this function with wrong types would be the same

#

but it's my bad to ask that as I should just.... test it 😄 which I plan to

maiden pawn
#

using Any is bad thing that preferably should be never ever used

#

because it is kind of synonym for untyped anything

#

used only as crutch in the most difficult spots

#

using Any you forfeit static typing by Mypy in this place (and it propagates (very quickly) as untyped in other code places)

proper wind
#

fair. of course. my mistake to even give it as an example. I know about pydantic's Union for one type or the other, I believe there is a built-in similar way? At this point I am wasting your time with very simple things I can just google. I will look over all our messages again. thank you VERY much

maiden pawn
# proper wind fair. of course. my mistake to even give it as an example. I know about pydantic...

#type-hinting we have this place to ask typing questions
plus you can give a full reading to python typing documentation https://docs.python.org/3/library/typing.html

maiden pawn
#

but for test function above, better using generics because...

#
def test(num1: int | float,num2: int | float) -> int | float:
  return num1+num2

check this code for example

#

if num1 is int

#

you can still insert num2 as float

#

and return can be int | float or even error if conversion failed for some reason

#

in next code places this function will give you undecided int OR float

#

while you wish it being explicitely int for example if you submitted all ints into this function

#

here where Generics come in (concept you will learn how to use in Golang)

#
T = TypeVar('T', int, float)

class Summator(Generic[T]):
    def sum(self, a: T, b: T) -> T:
        return a+b
#

this code once used will propagate correct types

#

if u will submit into a int, you will have static typing error by mypy, if you will try not int into b

#

and it will correctly show static typed yield result as int if u submitted int, for other code places

#

Generic fixes type for multiple objects into same one basically.
making reusable function for multiple types used

#

this is concept of static typing... all types are known at compile time (before code is run) ^_^
this Generic stuff allows you to define them being known before code is run.
Since it can get known type of what you are going to submit, and thus calculating what will be type result before code is run

maiden pawn
#

It is again substitution though. In typed language it would not have been necessary to test

sick widget
#

Good evening people. I have a question, but I am quite new. Please feel free to delete my comment if it doesn't belong here. Does anyone use Kiwi TCMS?

summer lance
#

has anyone worked with adb or uiautomator?

tiny quartz
#

Hi is It safe to have asyncio_mode = true configuration and doesn't use decarator @pytest.mark.asyncio?

hexed cloak
haughty axle
maiden pawn
haughty axle
#

Running this through the debug option in pycharm

maiden pawn
# haughty axle ``` import pytest from django.test import TestCase class TestP(TestCase): @...

wtf is this pithink

use this or this.

import pytest

@pytest.mock.parametrize("arg1, arg2", [(1, 2), (1, 3)])
def test_p1(arg1, arg2):
   #something
    pass

@pytest.mark.parametrize("arg1, arg2", [(1, 2), (1, 3)])
class TestGrouped:
    def test_p2(self, arg1, arg2):
        #something
        pass

launching with pytest -k test_p

ensure to have pytest.ini and installed pytest-django

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
python_files = tests.py test_*.py *_tests.py
#

pytest parametrize for pytest tests, not for unittest

haughty axle
#

oh

maiden pawn
#

P.S. unittest tests are backward compatible with pytest. no need to refactor them all right away to migrate to pytest

haughty axle
#

Thanks

unkempt void
#

how to use airesponses if I built a custom class that uses ClientSession? I mock full URL and then receive ClientConnectionError? So my methods GET/PUT/DELETE just use self._session.get/put/delete inside them. Not even an overloading

sly cargo
#

Hello everyone, I'm new here and I wanted to ask a question about mutants that I'm having trouble with in an exercise, and I wanted to know where I can send this question within this Discord.

earnest sequoia
proud nebula
proper wind
#

Yes I am

#

always

proud nebula
smoky tangle
#

Hello there

#

any idea why my unit test doesn't get the environment variables but my actual program does ? especially when I write them in the docker-compose or dockerfile, they work for my program but now I declare them in the pycharm unittest configuration and it doesn't detect them

dense bough
#

What variables does it see?

smoky tangle
#

only the ones I write in a config.ini file

#

but it ignores the ones I send through pycharm

#

imma try the actual project one sec

#

I'm going to try something

smoky tangle
#

yeah so my actual django project doesn't ignore the environment variables when I execute the dockerfile through pycharm

#

but my unittest ignores the variables for some reason 😦

smoky tangle
#

so yeah

#

Imma investigate a bit more

dense bough
#

How does the test use the environment variables?

smoky tangle
#

so weird

#

it calls them and assigns them to variables and then throws an exception

#

if the variable doesn't exist

#

or it's empty after having assigned the value

#

that's why the test fails even tho it works on the actual program

dense bough
#

Is the test for function behaviour based on existence of the environment variables?

smoky tangle
#

I want to send the environment variables through pycharm for the test so I don't have to write them hardcoded on a config file

#

kind of

#

the test calls a program function that actually verifies the variables exist, the function throws an exception if something doesn't exist and the test fails if the exception in that function is thrown

#

and passes if nothing in that function is thrown (everything exists)

#

so it is really weird

#

I have already informed the senior so I'm chilling now looking at something else but it's really weirddddddddddd

upbeat bay
#

Hi, I want to create class and package diagram of my project with pyreverse. It fails continuously. Is there anyone could help ?

elder blaze
heavy topaz
#

im new to unit testing can anyone help me with some lines of code quick

#
import time
from selenium.webdriver.common.by import By
from credentials import mojUsername
from credentials import mojPassword

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("disable-notifications")
chrome_options.add_argument("disable-popup-blocking")
driver = webdriver.Chrome(options=chrome_options)

driver.get('https://www.klix.ba')
driver.maximize_window()

time.sleep(4)

header = driver.find_element(By.TAG_NAME, "header")
loginButton = header.find_element(By.ID, "user")
loginButton.click()

time.sleep(2)

authenticationModal = driver.find_element(By.ID, "authentication-modal")

username = authenticationModal.find_element(By.NAME, "username")
password = authenticationModal.find_element(By.ID, "lpassword")
submit_button = authenticationModal.find_element(By.CSS_SELECTOR, 'button[type="submit"]')

username.send_keys(mojUsername())
password.send_keys(mojPassword())
submit_button.click()

time.sleep(3)

sportBtn = driver.find_element(By.LINK_TEXT, "Sport")
sportBtn.click()

time.sleep(3)

vijest = driver.find_element(By.LINK_TEXT, "Đoković je apsolutni rekorder po zaradi, ima čak 32 miliona eura više od prvog pratitelja")
vijest.click()

time.sleep(3)


sviAElementi = driver.find_elements(By.TAG_NAME, "a")
for aElement in sviAElementi:
    if "prikaži sve komentare" in aElement.text.lower():
        aElement.click()
        break

commentWall = driver.find_element(By.ID, 'comments_display')

for wall in commentWall:
    comments = wall.find_elements(By.CLASS_NAME, 'komentar')
    likeButtons = wall.find_elements(By.CLASS_NAME, 'voteUp')

    for comment, likeButton in zip(comments, likeButtons):
        if "Cestitam" in comment.text.lower():
            likeButton.click()
            pass

time.sleep(5)```
#

it just closes out and doesnt click the like button

hazy rampart
#

hi, feeling difficult in understanding the below code.

from unittest.mock import patch
from fetch_data_from_db import *
from unittest.mock import MagicMock

def configure_connection(mock_conections: MagicMock, mock_cursor_context_manager: MagicMock):
    mock_cursor = MagicMock()
    mock_cursor.__enter__.return_value = mock_cursor_context_manager
    
    mock_conn = MagicMock()
    mock_conn.cursor.return_value = mock_cursor

    mock_conections.__getitem__.side_effect = {"default":mock_conn}.__getitem__

@patch("common.db.connections")
def test_manager(self, mock_connections):
    mock_cursor_context_manager = MagicMock()

    def execute_side_effect(query):
        expected_query = """
        stub_for_expected_query
        """
        if query == expected_query:
            mock_cursor_context_manager.description = ["column_names"]
            mock_cursor_context_manager.description = ["values"]
        else:
            mock_cursor_context_manager.description = []
            mock_cursor_context_manager.description = []
    
    mock_cursor_context_manager.execute.side_effect = execute_side_effect
    configure_connection(mock_connections, mock_cursor_context_manager)

can anyone please explain..

fervent blaze
#

Hello folks! I have some old Selenium tests at my job that we’re looking to start running again. I understand that Selenium IDE is deprecated, but I was able to run the tests on the Chrome extension.

Would it be alright to look into using WebDriver or are there any other alternatives?
I’m thinking we will have to toss the tests, but if it’s possible to use them still then that would be great.

Thank you in advance.

atomic raven
#

How do I deal with functions that use floating point numbers and hence may lose precision when doing unit tests? Do I simply give the result a margin of error?

lament heath
#

I know this one is about unit testing but we were playing around with the idea of somehow gathering coverage on e2e tests for web services.

@river pilot have you ever seen anyone experimenting with such crazy ideas?

atomic raven
river pilot
river pilot
lament heath
#

Holy crap, it's that simple.

#

You're a rockstar!

#

Thanks!

proud nebula
old venture
#

guys, do you have any ideas of using AI or ML in API testing?

lament heath
#

Testing should be precise. ML is not exactly precise

maiden pawn
# old venture guys, do you have any ideas of using AI or ML in API testing?

Yeah. Best way: not using it.
Because it will be to meaningless, noisy and super highly implementation details attached anyway even if it did smth.
you will get a lot of fragile test tech debt, that will be all the time breaking and making you dissapointed in testing and preventing any refactoring (moving you to direction opposite from the why tests were needed in the first place)

proud nebula
#

I mean, if you don't know how to write python syntax I think it can help a lot. But otherwise it seems bad to me....

#

I think I'd consider ChatGPT if I wanted to write code in a language that was new to me but the language was well represented in the training set of ChatGPT.

old venture
#

I agree with @maiden pawn and @lament heath , thanks for ur words

#

I tried explaining this to my higher ups much before, but he is not understanding
Seeing chatGPT he wants some product for API testing with Ai

Btw, this a place where i intern, i wanna quit cuz of this, what do i even do

proud nebula
old venture
proud nebula
#

That's the part that verifies that the code does what you want. You can then use AI tools to generate the code to test maybe (although I'm skeptical about that)

old venture
proud nebula
#

(plus some hallucination noise of course)

old venture
#

Like say my application has id, of the form UHJXXXX where X denote some integer,
But an AI model would generate inteegers for id as test cases, like id=2 id=35

old venture
#

He will prolly tell i did it wrong but whatever

proud nebula
#

I mean, just try it yea. Maybe you can use it to generate a bunch of the code then fix it.

#

Try it for an hour, then you can be specific in your push back.

ember maple
#

Can we stop calling super size Ego t9 ai, those models are text predictors that are simply so huge and powerful that the output may seem reasonable, and their output is Sometimes immensely helpful, but the errors are painfully cringe as well

proud nebula
maiden pawn
maiden pawn
#

@old venture a bit of jokes on topic

proud nebula
#

It's showing the html yea. What did you expect it to show?

ember maple
proud nebula
#

It does show where the error is. And it helpfully shows the string that string isn't found in.

#

I mean.. unit testing is just python code, it's not magic.

jolly tangle
#

I am getting a weird error in pytest and flask with ckeditor . Here is the post I made on stackoverflow with more detail. https://stackoverflow.com/questions/76486946/i-am-getting-an-error-when-running-pytest-but-the-code-is-working-when-i-use-fla . If anyone knows the answer ping me please and thank you.

proud nebula
jolly tangle
#

I am confused what it is saying.

#

Can you please explain?

proud nebula
jolly tangle
#

But in the register call I don't use it. At least I don't think I do.

#

@proud nebula I assume @auth.route("/register", methods = ['POST', 'GET']) def register() is not the call of the register function?

#

Is return render_template(register.html) the call of the register function. Because I don't need to pass anything ckeditor because that makes no sense.

proud nebula
#

I'd probably put a breakpoint and check the first time it gets there and how it's different from the second

jolly tangle
#

I think I know what the problem is now.
app = create_app(PytestConfig), is located at 2 locations in pytest.

PytestConfig is in config.py and I have multiple config like the example below. If I turn app = create_app(PytestConfig) into a fixture do you think that would work even though it is located at 2 examples?

https://flask.palletsprojects.com/en/2.3.x/config/

class Config(object):
    TESTING = False

class ProductionConfig(Config):
    DATABASE_URI = 'mysql://user@localhost/foo'

class DevelopmentConfig(Config):
    DATABASE_URI = "sqlite:////tmp/foo.db"

class TestingConfig(Config):
    DATABASE_URI = 'sqlite:///:memory:'
    TESTING = True
jolly tangle
#

Thank you and hopefully that fixes it.

proud nebula
#

can a flask app be destroyed when you're done with it or something?

#

Maybe that could also be a solution...

jolly tangle
#

I don't know . I never heard of anything like that, so I would have to google. I was just thinking of putting the fixture in app =.....
You can delete the db and I do that in a fixture. I am going to test the fixture now.

proud nebula
jolly tangle
#

Sorry just moving some code around still testing thanks for the suggestions

jolly tangle
#

I am trying something but parts are still not working.

tests/conftest.py

Here the code is working.


@pytest.fixture()
def create_app():
    app = create_app(PytestConfig)
    return app 


@pytest.fixture()
def create_db(create_app):
    db = SQLAlchemy(create_app)
    return db   

app = create_app
db = create_db(app)

# app and db are used in the below in the code.
... 

The problem is I need db for the class usertest. But I am getting an error. I tried a few combos of importing the fixtures to function but I assume I am screwing up.

test/models.py

def create_db_again(create_db):
    return create_db

db = create_db_again
db = create_db_again


class UserTest(UserMixin, db.Model):
...

I will update the error if the below doesn't work


How do I pass on the fixture to the functions. Unless someone thinks I should try something else. I am currently trying creating and teardown/destroy the app.
jolly tangle
#

I think I got it working I got rid of the fixture forcreate_db and just imported from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy()

lament heath
#

Please, read pytest's docs about fixtures. You are using fixtures incorrectly.

lament heath
jolly tangle
#

Okay I will thanks my example is above is a mess because I thought it was like db = sqlalchemy(app) but is like db = sqlalchemy() Hopefully this fixes the original error with ckeditor

jolly tangle
#

I got ckeditor error working I can share how if anyone is curious

jolly tangle
#

Can I parametrize and pass it on to fixture? Then how would I pass on the fixture with the param to a another fixture? Assuming this is possible

lament heath
#

pytest-fixture-classes allows you to do factory fixtures rather easily.

jolly tangle
#

When you say "pytest-fixture-classes" , do you mean classes or fixtures? sorry for the dumb question

lament heath
#

Fixture classes. It's both fixtures and classes.

#

But I can't be sure if it solves your problem until you describe what you want.

modern hedge
hexed cloak
#

You probably don't want to use pytest via pipx

modern hedge
#

i think this should be reported as an issue. didn't pytest undergo a lot of work to be pipx compatible? or was it tox?

modern hedge
modern hedge
#

oww

#

it was tox for sure

#

underwent a major version change

hexed cloak
#

tox 3 worked with pipx

modern hedge
#

not as good perhaps

#

i didn't try it really tho

river pilot
velvet pollen
#

Hello
I have a function like this that inserts an award in a table

def insert_award(award):
    with contextmanager(get_db)() as session:
        obj_db = Award(**award)
        obj_db.created_at = datetime.utcnow()
        session.add(obj_db)
        session.commit()
        session.refresh(obj_db)

related functions to create the engine and link thee session with the db

SessionLocal = None
if app_settings.database_url:
    engine = create_engine(app_settings.database_url)
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)



def get_db() -> Generator:
    try:
        db = None
        if SessionLocal:
            db = SessionLocal()

        yield db
    finally:
        if db:
            db.close()

im trying to patch this get_db in ccontext manager (or the session, or whatever can be patched at this point) like this

@patch("app.db.session.get_db", new=mock_get_db)
def test_insert_award():
    insert_award(award)

I have tried almost every variation I can think of this including using monkeypatch, patch.object creating a with block but no matter what it always uses the get_db instead of the mock_get_db function (which is the same function but has a different url)

Anyone has an idea what can I do here?

modern hedge
#

Keeping everything in the venv is a space waste for one, and you need to keep it updated for every venv

river pilot
weary quarry
#

Same here. It is quite nice to have the freedom of deleting the venv and rebuilding it with a simple makefile. Refreshing to reset the stage easily.

proud nebula
pseudo ermine
#

Can anyone recommend me a GUI testing framework/tool for creating automated unit tests for pyQt GUIs? It doesn't have to be free but something like the official qt QA tool which costs several hundred dollars per month is out of my budget.

viscid basalt
#

You can specify different engine here, or a live connection

#

e.g.

async with engine.connect() as conn:
    transaction = await conn.begin()
    SessionLocal.configure(bind=conn)  
velvet pollen
viscid basalt
velvet pollen
#

Same thing, I cannot modify anything in the original code

#

well, actually that was before, I had a talk with my tech leader and we decided another approach. I don't think it was possible to do it the way it was planned originally

modern hedge
molten escarp
ember maple
ember maple
# molten escarp What's the footgun?

Python is a hot reload hostile language and it compound's with none of the pytest internals being reload aware, so there's quite some potential for mayhem on anything that's using import Time registration or metaprogramming and one has to play whack a mole

molten escarp
molten escarp
ember maple
molten escarp
proud nebula
drifting sorrel
#

Maybe I’m misunderstanding the problem, but if there’s a footgun in there - wouldn’t writing unit tests with depending state be the actual one (and not hot reloading per se)?

ember maple
drifting sorrel
ember maple
drifting sorrel
ember maple
forest maple
#

i am getting this while running unit test for fastapi app using pytest.

`E RuntimeError: Task <Task pending name='anyio.from_thread.BlockingPortal._call_func' coro=<BlockingPortal._call_func() running at /home/miicon/Documents/services/protocol_cs/venv/lib/python3.10/site-packages/anyio/from_thread.py:217> cb=[TaskGroup._spawn.<locals>.task_done() at /home/miicon/Documents/services/protocol_cs/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py:661]> got Future <Future pending cb=[Protocol._on_waiter_completed()]> attached to a different loop

asyncpg/protocol/protocol.pyx:338: RuntimeError
==================================================================== short test summary info ====================================================================
FAILED tests/test_routes.py::test_invalid_email_login - RuntimeError: Task <Task pending name='anyio.from_thread.BlockingPortal._call_func' coro=<BlockingPortal._call_func() running at /home/miicon/Documents/serv...`

the tests that i was running:
`def test_successful_login():
data = {'email': 'k@gmail.com', 'password': '123'}
response = client.post("/users/sign-in/", json=data)
assert response.status_code == 200

def test_invalid_email_login():
data = {'email': 'icuhroj@gmail.com', 'password': '123'}
response = client.post("/users/sign-in/", json=data)
assert response.status_code == 401`

pls help
my views are async and tests works if i run these two test separately. how can i solve this

proud nebula
ember maple
proud nebula
ember maple
proud nebula
#

like how "REST" means "json over http" nowadays, even though that was not at all what the original inventor of the term meant

#

Sometimes it's useful to fight for the words, sometimes it's best to just accept the common usage even if it's a bit stupid :P

#

"Give me the wisdom to know which is which" :P

ember maple
#

Well consistently letting Sales win the game on what words mean in Tech has a price

#

Just look what they did to agile

proud nebula
#

I guess.. depends on how wide you define "sales". If you include bloggers and "thoughtleaders" then yea 100%. But I think it's a bit more than that. At least with REST it was basically a marginally useful idea from the get go :P

ember maple
hexed cloak
tawdry dew
#

hello guys,
any suggestion about regression test, when the services use redis ?
the communication between service is rpush / blpop

the main problem is, the function in python script just return void (but in flow function is use rpush/blpop)

how i assume the past is correct (talk about data) if function just return void / None

molten escarp
# ember maple They involuntary are, smaller unittests are so quick that hot reload has no.wins

Not exactly, a small unit test SHOULD be quick, however, with a django monlith, this happens:

  • Tests are discovered by loading all 10,000+ of your ball-of-mud monolith's files
  • Your 2,000+ migrations are imported so that the final state can be computed, even though you've disabled migrations already
  • The test database is built, if needed.

So then your simple test with one model ends up taking 45 seconds to run (0.05 seconds actual test time)

To make things worse, many developers have run tests out of a bind mounted docker container unaware of how slow it makes it, so the I/O is horrendous. That 45 seconds turns into something much worse, and I haven't even mentioned running with the debugger on.

ember maple
#

Can we coin this scenario a unittest that has gone to the systems Test hill to die

molten escarp
#

Hey if you got a silver bullet I'm sure its worth a lot of money

#

Actually, hot reloading is probably the closest thing and I'm not getting paid squat.

ember maple
ember maple
proud nebula
molten escarp
#

You can and should squash old migrations but it's not perfect and will still eat up time

#

I was stating the migrations are still imported and they absolutely are. They usually are not ran but some devs stuff "static" data into a migration and build tests against it and then you gotta run them until someone gets around to fixing that.

#

There's also migrations that enable database features, etc

proud nebula
#

I use it, and it's great

proud nebula
#

Just don't look in the migrations folder and you'll be happier :P

molten escarp
#

I'm going to hold off on doubling down until I get a chance to recheck that things behave the same as they did a year ago when I originally investigated the issue. Back then, if you set it to SkipMigrations there was still a long delay as it went through the migration step even though it wasn't running migrations.

#

I have an issue to verify the hot reloading helps with that and I just haven't gotten to it yet

proud nebula
#

hmm, that's disturbing

#

ran one fast test now and it took ~1.1s total for pytest.

#

I put a hard crash in a migration for that app and it was never triggered though, so I don't think that's why pytest was that slow

#

meh, and hammett doesn't work on this project. Will have to look at that later.

molten escarp
#

Oh neat, I wasn't aware of hammett

#

I have a to do item to create a nose style test discovery plugin but maybe hammett obsoletes that?

proud nebula
#

some fiddling later... got one test to run in 913ms in hammett. Better than pytest 1.3s but still surprisingly slow.

proud nebula
#

it does some evil stuff to get that compatibility to work heh. sys.modules['pytest'] = hammett for example

molten escarp
#

The primary use case for the nose test discovery is VS code's auto test discover on save

#

It's a bit obnoxious when test discovery takes over a minute

proud nebula
molten escarp
#

Nose tests traverses the file system to look for tests based on filename convention and then class / function name convention

#

I'm contrast, pytest actually executes the test code. In many cases the results are often identical except that nose can't easily support extracting parametrized tests

#

Since parametrized tests could come from a run time variable

#

PyCharm runs its own logic which is basically nose test discovery on the file you're looking at. I don't think it supports parametrized tests either (you run all subtests rather than being able to target just one)

proud nebula
molten escarp
#

I'm pretty sure pytest --collect-only will import the test code

proud nebula
molten escarp
#

I'm not sure about individual file name behavior. VS code isn't that smart

#

Hanmett caches it though?

proud nebula
molten escarp
#

Right but PyCharm is using custom logic to know that's a test in the first place.

proud nebula
molten escarp
#

Oh

#

So maybe he's still a need to build a nose style test discovery plugin then

proud nebula
proud nebula
molten escarp
#

Because in VS Code it doesn't have any logic. It calls pytest --collect-only at the root level and then parses the output. If you write a test and then want to run it, you have to wait for that to finish if you want to use the UI

proud nebula
#

The few false positives will be worth it a thousand times over.

molten escarp
#

Well the regex is basically the nose plugin I would write

#

There's also tests that inherit from a class with TestCase in the name

#

That covers most of them

#

The last part is converting the pytest output so that the subtests are used to create a pass / fail result for the parametrized test

proud nebula
molten escarp
proud nebula
#

That has false positives if people name functions test_ that aren't tests.

molten escarp
#

Ah yeah, that makes sense

proud nebula
#

Imo that's much better.

#

Will be crazy fast too.

molten escarp
#

Yeah that might be better. The containing class still needs to be captured but one can skip looking at the parent class

proud nebula
#

Or maybe another really stupid regex :)

#

You can be fancy and use the ast module too. It's quite fast.

molten escarp
#

ast requires importing the code though?

proud nebula
molten escarp
#

I suspect parsing text is faster

proud nebula
forest maple
proud nebula
forest maple
slender harness
#

So for work we use pytest, and I'm coming across some extremely weird behavior
I have a main function defined in the file named handler.py like so:

from package.collector import Collector

def main(event, context):
  my_collector = Collector()
  registered, unregistered = my_collector.get_registrations()

and this is the test file for it:

from package.handler import main

import pytest

@pytest.fixture
def client_response():
  return {
    "item1": 1,
    "item2": 2,
  }

def test_collector(mocker, client_response):
  collector = mocker.patch("package.handler.Collector")
  collector.get_registrations.return_value = client_response
  main()

But the error I get is that the return value for get_registrations expected 2 outputs but got 0 - how is this possible if I'm forcing the return value to be something in the test?

proud nebula
slender harness
#

WHOOPS that's on me for messing up rewriting the code from the top of my head, this is what it should actually look like

from package.handler import main

import pytest

@pytest.fixture
def client_response():
  return (
    {"item1": 1,}.
    {"item2": 2,}
  (

def test_collector(mocker, client_response):
  collector = mocker.patch("package.handler.Collector")
  collector.get_registrations.return_value = client_response
  main()

But the issue is still that despite me mocking the return value, I'm told that it returns nothing - not one or two variable,s it returns 0 variables

#

(expected 2, got 0)

proud nebula
slender harness
#

It's on my work machine that I can't copy code from, and I'm not about to rewrite it all
But that sampel is the main stuff - I am literally setting the return value and it's not actually returning anything

proud nebula
proud nebula
#

We can't possibly guess the problem when you can't produce the code or the error message

slender harness
#

ValueError: not enough values to unpack (expected 2, got 0)

proud nebula
#

have you used a debugger to step through and look at what happens?

#

another recommendation is to not use mocking and instead use dependency injection

slender harness
#

Gimme one second to boot up my work laptop and retype the relevant code

proud nebula
slender harness
#

That's just on my retyping in a hurry and from memory, I've already quadruple checked for syntax or other errors

#

and babysitting a needy dog rn lol

slender harness
#

main program (handler.py)

import logging

from package import Collector

from .constants import ACCOUNT_INFO

def main(event, context):
    ... # bunch of irrelevant code
    logger = logging.getLogger()
    logger.setLevel("DEBUG")

    # example owner object is written out in the test file
    owner = event["detail"]
    host_collector = Collector(logger, ACCOUNT_INFO, owner)
    # Collector initializes a varibale "self.owner_object" from the given "owner" object above

    ... # more unrelated code

    registered_hosts, unregistered_hosts = host_collector.get_registrations() # error happens here

test file

import logging

import pytest

from package import Collector

from package.handler import main

@pytest.fixture
def logger():
    return logging.getLogger()

@pytest.fixture
def example_owner_object():
    return {
        "HOST GORUP": [
            {
                "class": "some class",
                "hostname": "host.domain.com"
                "found": "2022-08-16T13:48:01.440623",
            },
            ... # another item like the one above
        ]
    }

@pytest.fixture
def colector_operations(logger, mocker, example_owner_object):
    mock_collector = mocker.path("package.handler.Collector")
    
    mock_collector.owner_object = example_owner_object
    mock_collector.get_registrations.return_value = (
        [{"host1": "1234"}, {"host2": "5678"},],
        [{"host3": "1738"},],
    )
    # also tried setting it with a fixture

    return mock_collector
    # also tried the following
    # collector_instance = Collector(logger, "1234", example_owner_object)
    # return collector_instance
hexed cloak
slender harness
#

I mean it's internal AWS code so no

#

I realize I can't step through and debug the pytest process because it's run through another internal AWS tool that does a lot of wrapping and such around the environment

#

I did do a silly debug step and used inspect.getsource for the function that's failing and it does come back fine in the test run

proud nebula
slender harness
#

I mean it's AWS, everything is internal lol

#

I'd normally go to my coworkers for help but the ones who worked on this project / related stuff are all away until wednesday

slender harness
#

Not sure what jobs let you guys just copy paste internal code to the public

slender harness
#

Well I ended up solving the issue
I had one variable in the Collector class that I was mocking elsewhere. That variable being mocked was being used in my functions, but also in other ones within the class. Long story short, the Collector class had method A which called method B which used that mocked variable, which would cause B to fail and thus A to fail

molten escarp
#

This is why I will advocate:

  • go make a dev container
  • make your dev container support everything a dev would need to do for the project
  • yes it takes time. Better to do it once than for every engineer
slender harness
#

I mean do you want the run down of how we do things in AWS

  1. Every dev has a dedicated dev machine we remote into through our IDE / code editors
  2. We set it up to use the proprietary internal AWS dev tools (look up an app called brazil, there's a few writeups online about it)
  3. each package has what's called a "version set" which lists out any dependencies you have in terms of other AWS packages (not regular dependencies like a 3rd party python library, this specifically is for listing out AWS packages you need)

brazil acts kind of like venv or poetry but works on multiple different languages, and you don't enter into a shell environment like you would with venv's activate or poetry's shell commands. Instead you define a psuedo makefile to describe actions you want to take (IE format would run black, isort, flake8, etc. and another command test would run your unit tests).
When you run those commands, it runs them in a contained instance that you can't enter - so running format would do the specified actions and exit that environment immediately.
So there is no stepping into the code like you normally would with a program, at least that I've seen, but I've also only been here for 6 months and there are infinitely many things to learn here

molten escarp
#

There's an option to wait for the debugger to attach prior to actually running

#

Every dev has a dedicated dev machine we remote into through our IDE / code editors
This is a good opportunity to use dev containers. You can install python on the dev container and have the python code run out of that. The dev container can also install other languages, and tools. Its a bit more standard to the direction to what the software industry is moving in. This frees you from running commands through another command, which creates incompatibilities.

proud nebula
viscid basalt
weary quarry
#

A dev container advocates the environment is the same between two machines and reduces the "works on my machine" syndromes. It's not bad. It's one tool of many to unify vastly varying dev environments.

viscid basalt
#

IMO working in container is just a bad dev experience (at least for me)

#

All you usually need is to populate .env and set up dependencies

weary quarry
#

In a simply setup, yes that is where it starts.

molten escarp
#

A dev container is an actual standard which solves many problems. Running code is just one of them. It seems you are unaware of that. https://containers.dev/

viscid basalt
#

That's just not something I really need since it doesn't solve any problems for me

molten escarp
#

Everyone says that until they move to a new project and wish they made it ahead of time

#

I got some Java for you to write. Are you set up for it?

viscid basalt
weary quarry
#

I think the general topic was a wide scale development challenge.

viscid basalt
#

So, no

molten escarp
#

Are your linters set up to automaticaly run and match the CI

#

Do you have the URLs to production logs?

#

We have some tools that help with managing distributed tasks. Do you have that set up too?

#

kind of see where this is going?

viscid basalt
#

Our linters match the CI, and I don't really get the other points you're trying to make

molten escarp
#

I can walk to the grocery store but a car is faster. Does that mean I have no need for a car?

#

Your configuration is completely static and never changes?

viscid basalt
#

We run a set of linters, if that set is changed I can add new one into CI, configuration is stored in pyproject.toml

#

You can run linters locally via single command

[tool.pdm.scripts]
lint = {composite = [
    "black .",
    "ruff . --fix",
    "mypy .",
]}
slender harness
#

I would say there's definitely a way to step through the code that I don't know about - most of the work I've done was in another project that I didn't find the need to do that and I only recently started the one I mentioned the code of before

proud nebula
slender harness
#

Don't get me wrong, I am going to research it more and see why things are the way there are
I'm fairly certain that there are ways to step through code among many other things, but despite being 6 months in I'm still considered extremely new in terms of how much I know about the tools and such

#

First 3 months were dedicated entirely to training and learning stuff and I'm still nowhere near knowledgeable enough on things here
Anyways lets steer back to the channel topic

median barn
#

I am thinking to give a learning session on pytest.
What do you guys think are the topic that needs to be included for that??
Any suggestions please???

weary quarry
#

Writing small, simple tests.

fierce sundial
#

Hey all, can someone help me write tests for celery tasks that are started from fastapi requests in a docker container

magic lagoon
#

Ma

kind meadow
slender harness
#

Have not tried but I'll look into it on Monday

jolly tangle
#

Hey 2 part question,

I have a file tree like below.
I tried pytest -q --capture=no , but the functions in test_donation_functions.py +test_register_functions.py do not print.
How do I print in the files mentioned above?


│   ├───tests
│   │   │   models.py
│   │   │   __init__.py
│   │   │
│   │   ├───donations_tests
│   │   │   │   conftest.py
│   │   │   │   test_donation_functions.py
│   │   │   │   __init__.py
│   │   │   │
│   │   │
│   │   │
│   │   │
│   │   ├───non_db_functions
│   │   │   │   conftest.py
│   │   │   │   test_models.py
│   │   │   │   test_password_function.py
│   │   │   │   test_routes.py
│   │   │   │   __init__.py
│   │   │   │
│   │   │
│   │   ├───register_tests
│   │   │   │   conftest.py
│   │   │   │   test_register_functions.py
│   │   │   │   __init__.py
│   │   │   │

Also will the print statement show up in the example below. I realize below is just a simple example but my question still stands.

Thanks for the help.

def test_some_function():

   print('some string')
   assert 1 == 2 
viscid basalt
jolly tangle
#

@viscid basalt
test_...

#

Yes I mean tests instead of functions I wasn't aware of the terminology

viscid basalt
#

Run pytest -vv and check if these tests are running?

jolly tangle
#

Sorry they discover them but I want the print statements to run

#

in the different tests

viscid basalt
#

--capture=no or-s should work 🤔

#

Why do you need to print btw?

jolly tangle
#

pytest -q --capture=no , I tried

#

just some extra info so if the functions work print('function works ') else print('function does not works ') This would be in if or else statement.
Its not essential but it is nice if it works

viscid basalt
jolly tangle
#

So you think don't worry about it?

viscid basalt
#

If you need to add some extra info to your asserts you can add comma and a string:

assert func() == 42, "Should be equal to 42"
jolly tangle
#

Ya I guess that works thanks

viscid basalt
#

But really - prints should work

jolly tangle
#

It doesn;t

#

I can upload my code tomorrow

viscid basalt
#

Can you create a minimal reproducable example (mro)?

jolly tangle
#

I think it is easier if I link my github , it is getting late I will do it tomorrow do you mind if I at your name tomorrow here?

viscid basalt
#

Sure

jolly tangle
#

thank you

#

bye

jolly tangle
#

@viscid basalt My mistake it seems to be printing

dreamy arrow
#

how does testing in async work?

viscid basalt
#

like anyio or pytest-asyncio

lament marten
#

Hey folks. I can't wrap my head around the usage of 'class' scoped pytest fixtures. Assume I have a pytest fixture that is having the class scope. I also have a few test methods as well grouped under a class. So if I need to use this class based fixture, do I need to pass it as a parameter to each and every test in that class? Or should I just add the usefixtures('fixt_name') above the class name and continue working?

If former is the case, then would the class scoped fixture be called each time, the test runs? In that case wouldn't the definition of class scoped fixture - will run only once per class, be broken?

@pytest.fixture(scope='class')
def class_fixt():
  pass

Should I use the class fixture this way ::

class ABC:
  
  def test_1(self, class_fixt):
    pass

  def test_2(self, class_fixt):
    pass

or

@pytest.mark.usefixtures("class_fixt")
class ABC:
  
  def test_1(self):
    pass

  def test_2(self):
    pass
ember maple
lament marten
lament marten
ember maple
#

bascially if the fixture yields a object you directly use - its considered good practce to take it as a parameter and use it so its easy to see where things are from

lament marten
ember maple
#

fixture with class scope get cached at class scope, so it gets made once for all the tests that use it and belong to the right scope/parameter set

#

and for example if you have a class scope fixture with params, then pytest reorders the tests so it can hold onto it as long as sensible

#

so fixture scopes allow caching, and reorder tests to make effective use of those caches

lament marten
ember maple
#

there shoudlnt be - usefixture is basically pretending the test had a parameter to ensure the fixture is used for it

#

however make sure that fixtures dont do magic actions at a distance that depend on one another without having the fixtures also depend on one another

lament marten
#

So in my above code snippet, I can use the class scoped fixture in both the ways right?

earnest sequoia
#

I have a pytest plugin that is implemented as a class. Is there a way to use that plugin from the command line?

#

The documentation only covers plugins as modules

#

Apart from invoking pytest.main directly (which doesn't seem to interact well with coverage), I've managed to achieve it via this:

# conftest.py
plugin = MyPlugin()

pytest_addoption = plugin.pytest_addoption
pytest_sessionstart = plugin.pytest_sessionstart
pytest_sessionfinish = plugin.pytest_sessionfinish
#

but having to assign the attributes manually is not very DRY

#

Ideally, I would be able to just pass the plugin directly into pytest_plugins:

# conftest.py
pytest_plugins = [MyPlugin()]

but that doesn't work.

I've also tried referring to a local variable, like this:

# conftest.py
pytest_plugins = ['plugin']

plugin = MyPlugin()

which also fails.

ember maple
#

the only way to safely do this would be to register the instance in the pytest_plugin_registred hook of a early conftest

its painfull7error prone to implement plugins that do addoption/configure as class

instead have have configure/addoption at a module level and the class implementing the details be registred in pytest_configure

earnest sequoia
#

I see, thanks for the info

ember maple
earnest sequoia
#

Not sure whether I'm doing something wrong here, but the Hypothesis strategies generated by this helper class often break the data_too_large health check. Can someone take a look, if they have time?

#

!pastebin

bitter wadiBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

earnest sequoia
#

(st is shorthand for the import from hypothesis import strategies as st, the rest are available in the standard library and carry their usual meanings)

#

Example usage:

@dataclass
class Account:
    username: str  # Should be between 4 and 31 chars
    password: str  # Should be between 4 and 31 chars

st_account_attrs = AttrsStrategyBuilder(Account)

@st_account_attrs.register('username', unique=True, required=True)
def st_username():
    return st.text(min_size=4, max_size=31)

@st_account_attrs.register('password', required=True)
def st_password():
    return st.text(min_size=4, max_size=31)

st_account = st_account_attrs().map(lambda attrs: Account(**attrs))
lone flint
#

Is there's a way in unittest module, if i want to just test

def test_instance_creation (self):
        x = SomeClass(...)
river pilot
#

but you might want some assertions about x

lone flint
#

Cant think of reasonable assertion

proud nebula
lone flint
#

The class init contained a param which requires some validation object and that object had decorator over it which was too complex for me to go over and over again.
Yeah i don't need a test for it, i just ended up making another test case for that validator object.

real vale
#

I'm looking to improve my company's testing and could use some advice/direction. It's Django, so it's using unittest. Right now, it's a mess. Data is added to the db in nearly every test suites setUpTestData, so often we end up with tests that have to account for other tests data, making it really hard to run clean tests.

At a previous company, they used factories to setup all the data as a separate step, but I'm not sure what was done at the end of the tests to bring the database back to its original state.

I'd love some advice on how to tackle this, or what to look for. We've got a lot of models, so creating all those factories right off the get-go is going to be impossible (it makes for a good longer term goal). I'm trying to see if there's some way to run all the tests (or each test) as a transaction with rollback, so no test affects another one. This is where things get confusing, because from documentation, it seems like tests are already isolated, but that doesn't seem to be the case for data saved to the DB, which make sense. I'd want a rollback between each test if it's a transaction.

Am I missing something/not understanding something? How can I, and then my company, build better large scale test suites.

proud nebula
real vale
#

Yah, we're using mimesis for factories, but how will switching to pytest solve the transaction issue? This feels more of a structural thing than necessarily the package?

proud nebula
maiden pawn
#

that makes it highly optimizable to have the right amount of cleansing between tests for each stuff / with necessary level of reusage to keep performance up

#

at the same time pytest-django by default, makes sure that all database migrations are run only once for invoked testing process.

#

when i switch to another programming language, i immediately starting to miss lack of pytest in another language X 😆

#

factoryboy makes it even more powerful boilerplated and shorter in terms of unit testing code in the way that we have only the most relevant to unit test stuff to be present. highly promotes DRY setups regarding db testing and decreased noises from not important actions

loud latch
#

I'm having trouble getting pytests to recognize the module that I'm testing

#

My tests are in a separate directory from the module I'm trying to test, which I believe is normal, but that appears to be making it impossible for it to recognize the module I'm testing

weary quarry
#

What is the error? What does the directory structure look like? How are you importing what you are testing?

loud latch
#

The error is that a function (which I know exists in the .py file in the package I imported) isn't recognized. The top of my tests.py file is:

`import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(file), '..', 'src')))

from SwimScraper import *

print(SwimScraper)

def test_function():
df = getCollegeTeams(team_names = ['University of Pittsburgh', 'University of Louisville'])
assert len(df) > 0`

#

Whoops sorry

#

print(SwimScraper) prints the path to init.py in the correct directory. But when I run pytest SwimScraper/tests/tests.py , I get the following error: FAILED SwimScraper/tests/tests.py::test_function - NameError: name 'getCollegeTeams' is not defined. That function, and other functions I'd like to test, are in the in a file called SwimScraper.py that is in the same level of the init.py in the directory I'm importing

#

tests are in a separate directory from the source code. Here's the tree:
C:. ├───.pytest_cache │ └───v │ └───cache ├───build │ ├───bdist.win-amd64 │ └───lib │ └───SwimScraper ├───dist ├───src │ ├───SwimScraper │ │ └───__pycache__ │ └───SwimScraper.egg-info ├───tests │ ├───.pytest_cache │ │ └───v │ │ └───cache │ └───__pycache__ └───__pycache__

weary quarry
#

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))
This is asking for issues. Don't add src to the python path. Install the package you're building with pip -e . so that it's an editible install. No path muckier needed.

Star * imports are difficult to visualize what's being imported, try explictly importing what you are testing.

loud latch
#

pip -e?

weary quarry
#

-e, --editable <path/url> Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url.

#

If you install it into site-packages, imports "just work".

#

That's one of the points of the src/ nesting. To force you to test against an installed version.

loud latch
#

Oh, OK. So I rerun pip -e each time I've made a change I want to test?

weary quarry
#

No. The install is symlinked to your actual files. You change the files, the installed version is updated.

#

hence the --editable

loud latch
#

OK. It goes into my virtual environment I assume?

weary quarry
#

Yes. I assumed you were using one, I'm a little tired.

#

Works the same at a system level, just... at a system level.

#

venvs are better for cleanup.

loud latch
#

Right, OK. Does it matter that my setup.cfg file is in the directory that countains src and tests and build

#

Oh... I see how this all happened now. Thanks a ton!

jolly tangle
#

I am getting a error and I don't know how to fix the code. Can someone help? Also is there is a better way to test the code.
Here is the code https://paste.pythondiscord.com/4SQQ .

Please ping on reply.

proud nebula
#

from ..models import UserTest, db I highly recommend absolute imports

loud latch
#

Hm... Now I'm getting a module not found error. pip install -e . ran as expected. If I use python in my project, the statement import SwimScraper works as expected

loud latch
#

pytest

proud nebula
#

try the other one

loud latch
#

I think python -m pytest needs a file ending in _test.py to run properly, right?

proud nebula
#

nope

#

they are the same, except they handle python path slightly differently. Which is imo very stupid, but it's probably a big breaking change to fix that now.

loud latch
#

Well, I get the same error

proud nebula
#

yeaok. Worth a shot. Personally I don't like the src dir idea because of problems like this...

#

But it has other advantages I'm told.. just can't remember what they are right now :P

loud latch
#

OK, now I'm going crazy. It worked... once. Then stopped working. argh

#

Does pytest run tests in paralell?

proud nebula
#

better to look at something like testmon

jolly tangle
#

@proud nebula

"What is a SubRequest?" I have no idea.

When I run the code by typing pytest -q --capture=no I get the error


=========================================================== ERRORS =========================================================== 
_______________________________________________ ERROR collecting test session ________________________________________________ 
..\..\..\..\Anaconda3\envs\py\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
..\..\..\..\Anaconda3\envs\py\lib\site-packages\_pytest\assertion\rewrite.py:168: in exec_module
    exec(co, module.__dict__)
app\tests\login_tests\conftest.py:9: in <module>
    from tests.models import UserTest, db
E   ModuleNotFoundError: No module named 'tests'
================================================== short test summary info =================================================== 
ERROR  - ModuleNotFoundError: No module named 'tests'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
1 error in 1.81s

Here is my file tree.

https://imgur.com/a/ExntW7d

Please ping on reply.

proud nebula
jolly tangle
#

@proud nebula You are correct. I forgot to go from app.tests.models ... it seems to solve it and leave me with the original error

proud nebula
#

username_or_email = request.params seems like a weird line too... it makes no sense. It has singular on the left and plural (maybe a dict even?) on the right

#

username_form has a weird name too, it's not a form, but I guess it should be username_for_form?

loud latch
#

Should I be using logging? I need to view different variables to see why my tests in pytest are failing. I guess using print statements is not a normal thing for that?

loud latch
#

It's probably time to learn... Can you assert that there's an exception with pytest?

proud nebula
jolly tangle
#

@proud nebula

Here is the example I based the code on.
https://stackoverflow.com/questions/35413134/what-does-indirect-true-false-in-pytest-mark-parametrize-do-mean

Here is another example should I be using a class like the example below?

https://stackoverflow.com/questions/58319619/pytest-request-param-of-fixture-to-be-used-in-tests
I found this about the error, "AttributeError: SubRequest instance has no attribute 'param' "
https://github.com/pytest-dev/pytest-repeat/issues/2

But I already added autouse=True. How do I fix this?

Please ping on reply . Thanks for the responses

GitHub

Having trouble getting this plugin to work with a parametrized fixture like this: import pytest @pytest.fixture(params=['a']) def paramed_fixture(request): return request.param def test_use...

proud nebula
loud latch
#

Tests are like magic. I don't know why I didn't learn about this earlier

weary quarry
#

Magic? They are what you do at the terminal to test your code except they are automated.

jolly tangle
#

@proud nebula or anyone willing to respond , sorry to be a bother. I am wondering if I have to turn the code into a class.

Here is the example I am using https://stackoverflow.com/questions/35413134/what-does-indirect-true-false-in-pytest-mark-parametrize-do-mean

request = <SubRequest 'username_or_email' for <Function test_login_page_get>>

    @pytest.fixture
    def username_or_email(request):
>       return request.param  # username_form or email_form
E       AttributeError: 'SubRequest' object has no attribute 'param'

app\tests\login_tests\conftest.py:89: AttributeError

The code clearer doesn't have param attribute , but in the example they don't have param anywhere except in one location. Any advice?

proud nebula
jolly tangle
#

Thanks for the help. I just about got it running. I looked at the documentation for pytest specifically "request" and that helped a lot. I should have done that from the start .

sleek tangle
#

Is there a way to print "live" messages in pytest? As an example, I am using git lfs to store big data files used in regression tests. Since some of my colleagues are not experienced with git lfs, I wanted to automated it with a fixture. If the files are not present, pull them from git lfs.

However, if this happens, it will take longer the first time. Is there a way to indicate this when happening? Just so they are aware what's happeninf

maiden pawn
#

otherwise u can always write script wrapper around pytest

#

cross platform solution with nice interface

#

u can forward all additional custom pytest args automatically into it

sleek tangle
rustic patrol
maiden pawn
crude chasm
#

hello everyone

tall brook
#

Hi, I'm using python requests third party module to perform http calls to an external API. This API require an authentication so I'm using a class that implements this authentication based on requests.auth.AuthBase. This means that custom Auth class does a request to retrieve a token and then the actual call performs another request. I'm using requests.Session in both cases. For unit testing I use pytest and I'm creating mock objects using monkeypatch to avoid calling the actual API. My problem is when I'm creating unit tests for functionalities that depends on this call, I need to monkeypatch two requests.Session since they are in separate modules. It seems like monkeypatch override the last object I try to mock. So I have something like this

monkeypatch.setattr("client.requests.Session", client_requests_fail_mock)```

and inside `utils` module, the mocked `requests.Session` is actually the one from `client`, which is undesired. Any idea how can I avoid overriding mocked objects? Also, I'd like to stick to `monkeypatch` instead of using `unittest` module
#

basically, utils.requests.Session is behaving as client_requests_fail_mock. If I switch monkeypatch statement order, behavior also switches

drifting sorrel
tall brook
drifting sorrel
tall brook
drifting sorrel
tall brook
robust dome
#

Hello, how do I get the response from one unit test to another in order to do some operations on it? Is it possible to override default value of variable outside the unit tests in Pytest?

tall brook
robust dome
#

You can't pass an id as an argument when generating an object

tall brook
robust dome
#

I guess, I'm pretty new to automated tests

#

How do I do that? Using fixtures or cache?

tall brook
robust dome
#

Okay, thanks a lot

proud nebula
robust dome
proud nebula
ivory dirge
#

wsg @vast bobcat my mi

jolly tangle
#

I am trying to redirect and test that redirect in functions in flask and pytest. I have 2 functions. redirect_function which I plan on testing and redirect_function2 which represent a longer function. I shortened redirect_function2 just for temporarily making it easier to test but the output will be the same in the longer function.

When I run the code below I am getting an error.

def redirect_function(): 
    return redirect(url_for('auth.login'))

def redirect_function2(): 
    return redirect(url_for('auth.login'))

def test_login_functions():  
    assert redirect_function() == redirect_function2()

E RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

The error is a little longer but this is the gist.

Then when I add

from flask import redirect, url_for

def redirect_function(): 
    return redirect(url_for('auth.login'))

def redirect_function2(): 
    return redirect(url_for('auth.login'))

def test_login_functions():  
    #with test_app.test_request_context(): 
    
    with test_app.test_request_context():
            assert redirect_function() == redirect_function2()

Here is the error when I add with test_app.test_request_context(): in the example above.
test_app.test_request_context(): , is used for pytest instead of with test_app.test_request_context()

def test_login_functions():
    #with test_app.test_request_context():

    with test_app.test_request_context():
          assert redirect_function() == redirect_function2()

E assert <Response 218 bytes [302 FOUND]> == <Response 218 bytes [302 FOUND]>
E + where <Response 218 bytes [302 FOUND]> = redirect_function()
E + and <Response 218 bytes [302 FOUND]> = redirect_function2()

app\tests\login_tests\test_login_functions.py:27: AssertionError

lethal forum
#

Does anyone have idea about unit testing in FastAPI, I need assistance.

drifting sorrel
lethal forum
#

thanks for this link @drifting sorrel but I read those, and problem is these are not sufficient for my need,
like I created API (using FastAPI) but don't have any idea about testing it