#unit-testing

1 messages · Page 22 of 1

molten vapor
#

No I couldn't use that also

#

Maybe I can't use

jolly cape
#

U dont have authority to web scrape

sullen lark
#

what are you trying to configure?

molten vapor
buoyant bloom
#

there is probably an API for that

#

you wont be able to make it with beautifulsoup, etc. too many random id and classes in the webpages of Gmail & cie

cyan yacht
#

I am currently testing a Python scripts that interacts with GCP apis and a storage bucket. One of the methods check if a specific file exists in a given bucket: what's the best strategy to mock the bucket and mimic interactions with it?

tardy elm
#

Can you not put an assert inside of a for loop to make scalable testing using the unittest library? When I tried, self.assertTrue(True is False) passed

#

It seems like none of my tests are working as expected... Here's an example where I want to make sure these variables are part of the class and I have several test classes loaded up from different test.json files.

I know the last variable 'breaakddsa' is not in any of the three classes but it passes without issues.

class_variables = ['order_id', 'order_number', 'order_key', 'items', 'bill_to', 'ship_to', 'breaakddsa']
for test in self.testcases:
    for variable in class_variables:
        self.assertIn(variable, self.testcases[test]['object'].__dict__)
kind meadow
#

Are all these assertions independent of each other?

#

If so, you should use the subtest context manager

#

It will ensure that one assertion failing doesn't cause the loop to prematurely end

tardy elm
#

Ah, great. I'm really new to testing so I'll read into that

#

but any idea why it never fails even with ridiculous assertions?

kind meadow
#

Are you sure the assertion is even happening?

#

Or, more generally, that your test function is running at all?

tardy elm
#

Great question... Is there a good way to print debug? This unittest class seems to suppress printing

kind meadow
#

Do you know how to use a debugger?

tardy elm
#

I suppose that could work. One second

kind meadow
#

Yeah just set a breakpoint on the first line

tardy elm
#

Welp... Was able to figure it out. Thanks for all of the clues. I should have known something was up when I kept passing my tests without any problems 😛

tame folio
#

Hi All- I have a situation while writing my pytest automation script for my modules.
Scenario:
I want to write pytest for my module A and below are the steps I followed.
Step 1: I am importing module A in my pytest module
Step 2: actual Module A imports Module B and module B has dependencies file/code declared globally.
So the problem here is that - pytest runs only when I fix those dependencies in code specifically for pytest . But I don’t want to change existing code for this pytest.
Step 3: I found there is a concept called mock and I tried to mock B module by keeping them as sys.modules [‘module B’] = mock.Mock()
Step 4: the module B seems to be mocked up but the problem is my module A is a child of module B so when I mock module B , automatically I’m not able to use any functions in module A.
So at the end I’m not able to automate my module A in my pytest.
Can some one please suggest how to use mocking while importing with this inheritance problem or is there any other way for me import the module but ignore those globally declared dependencies in pytest.
Would appreciate if someone can help me here.

fiery arrow
tame folio
celest pilot
#

Hey, wondering how I can unit test this piece of code:

   # 2: Create employees
        employees = []
        for employee in input_dto.employees:
            employees.append(Employee.from_dict({
                **employee,
                'id': uuid.uuid4(),
                'calculation': calculation,
            }))

        self._emp_repo.create_employees(employees=employees)

I have mocked the emp_repo and I can assert that it has been called. But how would I assert that it has been called with the employees when they are created within the function?

acoustic roost
#

yo how can i find handles for such elements

#

submit type buttons

chilly yacht
#

can somebody show me how to connect a fixture to a test db (the test db is actually delpoyed on a server instance

#

like i have a test server running already, I'd rather just connect to that so i can do some assertions

tardy elm
#

When I run my unittests, I get a bunch of these warnings, although the tests pass
ResourceWarning: Enable tracemalloc to get the object allocation traceback

#

How can I enable tracemalloc?

#

The exact issue is: python ResourceWarning: unclosed <ssl.SSLSocket fd=2076, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.31.149', 57584), raddr=('54.243.167.193', 443)>

kind meadow
#

It can be enabled with the -X tracemalloc option for the interpreter

#

e.g. python -X tracemalloc -m unittest

broken silo
#

How can I use beautifulsoup to extract the content in the meta fields

#

the latitude and longitude

#

wrong channel, sorry 🙂

chilly yacht
#

this channel is so dead

inland sparrow
#

lol

#

can any1 help me

chilly yacht
#

i cant

#

i need help also

frail flume
#

can someone join me in help-aluminum. I have an issue with Django communicating to a postgresql database, i dont know why

robust plank
#

Does anyone have a good resource on how to learn selenium (or anything that works) to scrape some href tags from web pages sourced in javascript?

proper wind
#
def EvenOrOdd(number):
  if number % 2 == 0:
    print("Even")
  else:
    print("Odd")
#

!eval

bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

tawny bronze
#

I prefer using

def EvenOrOdd(n):
    assert isinstance(n, int)
    print("Odd" if n & 1 else "Odd")
split field
#

else "Even" you mean.

tawny bronze
#

yeal

proper wind
gilded cedar
#

@proper wind well have u ever considered that the list was empty

exotic glade
#

Hey guys I have a couple questions about Selenium, is this the right place to ask about it?

split field
#

Yes

inland sparrow
acoustic roost
#

yooo

#

i need soome help

#

regarding the data from a source on a site

marble ibex
#

How to scroll down

thin pilot
#
  File "v:/My-code-snippets/te.py", line 6, in <module>    
    a = a.append(random.randint(1, 10))
AttributeError: 'NoneType' object has no attribute 'append'``` 
what can be done for this error
```import random

i = 0
a = []
while i < 11:
    a = a.append(random.randint(1, 10))
    i += 1
print(f'The 10 random numbers are {a}')```
code
hollow basin
thin pilot
hollow basin
#

welp

#

!e

import random
a = random.choices(range(1, 10), k=11)
print(a)
bitter wadiBOT
#

@hollow basin :white_check_mark: Your eval job has completed with return code 0.

[8, 5, 8, 2, 3, 9, 1, 7, 2, 2, 7]
hollow basin
#

@thin pilot yay!

thin pilot
#

my modified code


i = 0
a = []
while i < 11:
    a.append(random.randint(1, 10))
    i += 1
print(f"The 10 random numbers are {', '.join([str(i)for i in a])}")```
#

!e


i = 0
a = []
while i < 11:
    a.append(random.randint(1, 10))
    i += 1
print(f"The 10 random numbers are {', '.join([str(i)for i in a])}")```
bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

slow zenith
#

Is it possible to export the session/cookie of a request into selenium to continue the process?

proper wind
#

Hello everyone, I would like to know what was the best framework to collect data from website scrapy or selenium website? And what’s difference between selenium and scrapy ? Someone help me please thank.

pearl cliff
#

@proper wind scrapy is for "crawling" and collecting information from lots of different webpages. selenium is a mock browser engine that can render css and javascript as well as html. it's a good idea to store your data in a database, or to save the data collected from each page in its own separate file on disk. there are advantages and disadvantages to each method, depending on what you are trying to do and what kind of data you are trying to collect.

#

im also not sure this is on-topic for the #unit-testing channel, unless you are using selenium to test your own website

crisp mauve
#

Do the above programs run on Pydroid 3 ( Python App )

drowsy osprey
#

hello guys, I am new to selenium, I have a doubt can someone tell me, how do I click on one element and go back , then click on another element of the same page, when i am trying to do that i am getting this: - selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

#

I have found some Stackoverflow answers but they are not working

#

also is this the right channel for this?? if not please guide me where to post this, thanks

midnight grove
#

I have problem with closing popup with selenium on: gg.pl. Can anyone help me with that?

#

my current code

#

driver = webdriver.Firefox(executable_path='D:\Python\Projekty\selenium\geckodriver.exe')
url = 'https://www.gg.pl/'
driver.get(url)
wait = WebDriverWait(driver,10)

wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[8]/div/div[2]/span')))
pop_up_click = driver.find_element_by_xpath("/html/body/div[8]/div/div[2]/span")

proper wind
#

guys how i can do if i got 10 proxy in my path .txt and once selenium use one proxy and after been used it use new one

drowsy osprey
#

ooh i also want to know this

#

maybe loop over them?

bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

grand hedge
#

I need help guys. How do you deal with lazy loading page? Selenium

chilly vigil
dusty dove
#

Driver.find_element_by_link_by_text(“Visit”)

torn pendant
#

how can i bypass website ads opening another tab when using selenium

tired hatch
#

i cant sigin in google with my selenium script using headless chrome

#

im trying with linux command line

#

"this browser or app may not be secure" help?

tall forge
chilly vigil
#

im trying to loop through a .txt file on a website in the search query and i want to program to recognize a button on the website and add the line in my text to another txt file + is found

kind meadow
cobalt karma
#

oop my bad I thought I'm in it sorry

torn pendant
iron plank
#

someone please help me

#

pleaaeeee

shrewd kraken
#

!ask

#

Really thought there'd be something on that command huh

tall forge
tall forge
tall igloo
#

Hey , any of you guys could give me some little help on a Selenium w/ python project im developing? Im facing some problems on some buttons I cant click on, or at least, they wont work

#

Im looking into auto buy some item, but even when all my elements exist, the button will get clicked but dont work, or at least the color changes the way ir does when clicked

whole hamlet
#

anyone know how to pass a config file/create a config file in a docker image

proper wind
#

Guys does anyone know how to deal with module structure with pytest? When i run my batteries Python doesn’t find the modules im trying to test or the modules which my tested modules are using, even when they are located at the same package

#

Actually my structure look pretty simple: root/core and root/tests

#

Core have some modules that contribute with the functionality of others so all of them im using simple import mudule_name

#

Then

#

Into my test modules i call into my import

#

from root.core.module_name import some_function

#

Python does not find root module when i run pytest and some cases seems the core modules can’t import another module which are siblings on that package

#

What im doing wrong here? I really appreciate the explanation

thorn rock
#

@proper wind

#

Hey ask here

reef swift
#

for those who dont know mathletics, its a dumb website which some schools demand their students to spend a couple of hours on throughout the week. being someone who doesnt enjoy math because the things they teach are literally for babies, I created a bot that does this assignment for me. It logs you in, presses play answers for questions 1-3 and I think 4. Then when it is over, it should play again. Also if an error occurs it just opens a new link and restarts. https://paste.pythondiscord.com/lunusizetu.apache

#

started it when I first started python and now its decent so all the math and stuff is quite slow and irratating but im kinda lazy so i dont bother messing with it.

slow zenith
#

Hello good day. Is there a way that I can add a pattern to network request blocking using selenium? Im using setNetworkBlockedURLs but it is not working since there are many urls and I cant add it on the script one by one

restive ether
#

Is there any way to get the name of the file which has been downloaded in selenium?

ornate inlet
#

dunno about selenium, but you could just listen to changes in a directory.

path = "downloads"
before = dict ([(f, None) for f in os.listdir (path)])

while True:
        time.sleep(1)
        after = dict ([(f, None) for f in os.listdir (path)])
        if before != after:
          for i in after:
            if i not in before:
               print(i)
          before = after
#

@restive ether

restive ether
#

I'll try this. Thanks.

ancient current
#

Aside from putting python [pytest] minversion = 6.0 addopts = -ra -q testpaths = test
in your pytest.ini , which should prevent pytest from going into your other directories, is there any other way to speed up the start time for pytest? The tests finish instantly (only 2 tests so far), but pytest takes its sweet time to get started.

#

I did have ; norecursedirs = ref env src .pytest_cache __pycache__ .git *.egg-info test/__pycache__
but i read that testpaths is better

rose oyster
rose oyster
whole hamlet
#

i figured it out I just wrote a config file and did a COPY

#

it was an internal dependency

rose oyster
#

oh, I see. you can also use ADD, i guess

whole hamlet
#

thanks for following up though appreciate it!

#

oh i'll take a look at that one

rose oyster
#

if you are using docker-compose, you can use your directory as a volume and it will synchronise everything for you

whole hamlet
#

understood thanks!

verbal spoke
#

Hello, how do I use Proxies w/ Selenium? To be exact, they are SSL Proxies.

indigo pier
#

hey guys so i have a downloadable link right here that when u click on it it automatically download the xlsx file. How can i open and save this file somewhere with python?

gilded coyote
#

hi guys

#

does anyone use pylint as Codechecker?

gilded coyote
gilded coyote
#

Something like

#

but i'am not familiar with Selenium

#

Why do you need a proxy for a testing tool?

fiery arrow
#

So people use proxies

#

That's, of course, not in line with our rules

#

!rule 5

bitter wadiBOT
#

5. Do not provide or request help on projects that may break laws, breach terms of services, be considered malicious or inappropriate. Do not help with ongoing exams. Do not provide or request solutions for graded assignments, although general guidance is okay.

fiery arrow
#

...and, in general, off-topic here.

#

This is a testing channel, not a webscraping channel.

gilded coyote
fiery arrow
#

nah, it's fine. just for future reference.

nimble orbit
#

Is getting site using proxy is possible with selenium? How do I do that?

fiery arrow
dark orbit
#

Hello. I want to do automation tasks with python. But I didn’t understand one point. How can I work my python file continuously for such as one year. The task of my python file is to send me a mail once a week. How can I do this?

nimble orbit
ocean mauve
lament loom
#

What module do you use for that?

fresh blade
wispy pivot
#

What

#

@verbal spoke pip3 is better

proper wind
#

!e

import turtle
turtle.tracer(0,0)
turtle.speed(99999999)
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(721):
    turtle.speed(99999999)
    t.pencolor(colors[x%1])
    t.width(x/100 + 1)
    t.forward(x)
    t.left(75)
    turtle.update()
bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

rain patio
#

!e

bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

crimson thunder
#

I think files that are imported rather than ran inherit the same args as the file thats importing it

untold plaza
#

does anyone here use arsenic?

#

pls ping :))

deep oyster
#

[pytest]
print("hello world")

patent stag
#
driver = webdriver.Chrome()
driver.get('http://automationpractice.com/index.php?controller=authentication')

driver.implicitly_wait(5)


x = 0
while 1>x:
    x += 1
    driver.implicitly_wait(5)
    emailSpot = driver.find_element_by_xpath('//*[@id="email"]')
    passwordSpot = driver.find_element_by_xpath('//*[@id="passwd"]')
    LoginButton = driver.find_element_by_xpath('//*[@id="SubmitLogin"]/span')

    driver.implicitly_wait(5)
    emailSpot.click()

    driver.implicitly_wait(5)
    emailSpot.send_keys("ff")

    driver.implicitly_wait(5)
    passwordSpot.click()

    driver.implicitly_wait(5)
    passwordSpot.send_keys('FF')

    driver.implicitly_wait(5)
    LoginButton.click()
    driver.implicitly_wait(5)
    try:
        Incorrect = driver.find_element_by_xpath('//*[@id="center_column"]/div[1]')
        if Incorrect.is_displayed():
            print("Incorrect!")
            driver.implicitly_wait(5)
            emailSpot.click()

            driver.implicitly_wait(5)
            emailSpot.send_keys(Keys.CONTROL, "A")
            driver.implicitly_wait(5)
            emailSpot.send_keys(Keys.DELETE)

            driver.implicitly_wait(5)
            passwordSpot.click()

            driver.implicitly_wait(5)
            passwordSpot.send_keys(Keys.CONTROL, "A")
            driver.implicitly_wait(5)
            passwordSpot.send_keys(Keys.DELETE)
    except:
        pass

Not sure why this does not delete the things in textbox

#

@ me

reef swift
#

try deleting until you get an error? or just delete the amount of letters in a text box @patent stag

#

Also instead of inserting an implicitly_wait which is not at all efficient try this. It will wait for an element to be visible.
from selenium.webdriver.support.ui import WebDriverWait wait = WebDriverWait(driver, 20) visible = EC.visibility_of_element_located example = wait.until(visible((By.XPATH,'xpath'))).click() or send()

untold plaza
#

why do i get error - arsenic has no attribute keys even though its present in the official documentation??

untold plaza
#

nvm i copied stuff from the module into program, works now

native osprey
#

hello guys, i am using allure+python for my automation. in logs, i am getting weird text when i use %(asctime). what can I change there?

#

text = "]32mINFO @[" something

hard monolith
#

How can I auto refresh a webpage until a certain element is visible? I tried using driver.refresh()
Time.sleep(2)
But that doesn’t work

proper wind
#

i like spaghetti code

frail depot
#

Has anyone here ever made macros(or some kind of Python equivalent) with python to make navigating around your computer easier/quicker? If so what did you make?

shut gorge
#

.

frail depot
#

sorry if dumb question

reef swift
#

so like u give a command in ur editor and it types something out for u?

hollow basin
#

How can I effectively write tests for non-deterministic code? Specifically, I want to test something I'm writing that is AI and the model not performing a certain way doesn't mean that the code is incorrect.

junior moss
#

While I want to test a web scraping app that can get rate limited

muted lichen
#

Like you don't test that

#

But what do you mean by AI

hollow basin
muted lichen
#

Are you using a trained algorithm to classifiy something or detect something in a signal/image

#

Pick a test case you know that the current version of model acts correctly with

#

Like if you were doing supervised learning, just use a training point that your model performs correctly

#

I don't know otherwise

hollow basin
#

It's bert for masked langugae modeling, if that means anything to you

#

my program has a generator that could yield up to a user-specified number of values or nothing depending on the inputs and the weights of the model.

muted lichen
#

I think this is a section that doesn't get tested conventionally

#

Like I don't know

#

All I could say is, for your current model weights, create a set of user inputs where you know apriori what your algorithm is going to put for each one

#

And then write a test that it actually does it

#

Does that make sense?

hollow basin
#

I would have to see if there's a way to pick a version of the model. Because as it stands, tensorflow automatically gets the model for you if you don't have it

#

and idk what logic it uses to decide which version to get, if it's even versioned.

muted lichen
#

Yeah ok

#

I'm not too familiar with natural language processing

#

So I'm not sure what you mean by you don't know your model weights

#

But it sounds like we are on the same page

hard monolith
#

how do i make a config file?

atomic copper
#

Is there a good way to test Pyqt5 apps?

proper wind
#
from random import randint
import os
import sys
clear = lambda: os.system('cls')
clear()
#Variables
player = False
def reroll():
    print("lol")

while player == False:
    dies = input (int("What type of die would you like to roll?: "))
    print(randint(1,dies))
    reroll()```
#

I can't figure out how to make this work

#

@proper wind

import os
import sys
clear = lambda: os.system('cls')
clear()
#Variables
player = False
def reroll():
    print("lol")

while player == False:
    dies = int(input("What type of die would you like to roll?: "))
    print(randint(1,dies))
    reroll()```

that should work
#

I fixed it B)

#

Oh, ok

#

thank you, though

#

I'm learning python

#

have fun!

#

thanks!

#
from random import randint
import os
import sys
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
clear = lambda: os.system('cls')
#Variables
die = False
def reroll():
    reroll2 = input(f"{Fore.CYAN}Would you like to reroll? [Y/N]").upper()
    if reroll2 == "Y":
        die = False
    elif reroll2 == "N":
        sys.exit()

while die == False:
    clear()
    number_input = input(f"{Fore.CYAN}What type of die would you like to roll?: ").isint()
    number = int(number_input)
    print(f"You rolled a {randint(1,number)}")
    reroll()
#

thats what it is now

#

I took the longer route

proper wind
#

Yo, can somebody help me with that code?

print("1 - Hello Bob")
x = input('Whats your number: ')
if x == "1":
    def f(name):
        print('hello', name)
    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()```
https://i.imgur.com/ivvfZlv.gif
You can see when i start it its come to input again.a
proper wind
#
from random import randint
import os
import sys
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
clear = lambda: os.system('cls')
#Variables
die = False
def reroll():
    reroll2 = input(f"{Fore.CYAN}Would you like to reroll? [Y/N]").upper()
    if reroll2 == "Y":
        die = False
    elif reroll2 == "N":
        sys.exit()

while die == False:
    clear()
    number_input = input(f"{Fore.CYAN}What type of die would you like to roll?: ")
    if number_input.isnumeric() == True:
        number = int(number_input)
        print(f"{Fore.CYAN}You rolled a {randint(1,number)}")
    elif number_input.isnumeric() == False:
        print(f"{Fore.RED}Thats not a number! >:(")```
how do i stop it from auto calling the reroll function?
#

if you try and do this, it works, even though no where in the code does it call it

proper wind
#

solved

proper wind
#

Hi all,

#

I've access errors

#

And I'm looking for good user agents for chrome webdriver

#

Any ideas?

#

I use selenium

zealous nimbus
#

reef swift
#

goooogle

fiery arrow
#

@proper wind Please don't paste random stuff in on-topic channels

proper wind
#

ok sorry

fallow merlin
#

Hi guys!
What is the best way to test an API with Django Rest Framework?

surreal elm
#

Hi may anyone help me automate a boring task with PDF? I have tried using pdfminer6 but can't seem to do it

proper wind
#

how do I make an exe out of a .py

hasty arch
tribal crown
#

Within pytest I need to test a function that takes filesystemObject as input and just read a value from filesystemObject.info.ftype

What is the easiest way to create this minimal object and set this value to something?

fiery arrow
#

An example of a python compiler would be Nuitka -- it turns Python into C that uses the CPython libraries.

hasty arch
stable spear
#

I want to write tests for a class, thats supposed to take in an audio stream and return a string (speech recognition)

#

could someone point me to some ressources on how to do that?

#

the important thing is that it simulates live audio and not just sending in wav files

supple ravine
#

Not sure if this is right place, but when I use getattr to get a class in another python module and instantiate, the abstract class doesnt enforce usage of abstract methods (I use abc.ABC). Any way around it in python?

#

Its sort of automated testing since it ensures that certain properties/methods are enforced in a class using inheritance

#

Oh nevermind, my bad, i didnt call the method using (), solved

mellow copper
#

i just started machine learning

twin pier
#

how can i run chrome with remote debugging as my main browser, or with the sessions from my main browser?

obtuse zealot
#

Can someone help me understand testing classes with unittest? I have no idea what I'm doing

#

and yes I have looked at the documentation

stable spear
#
import unittest
import foo

class TestFoo(unittest.TestCase):
  def test_spam(self):
    temp = foo.spam("Meaning of life?")
    self.assertEqual(temp, 42)

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

This would test the method spam from the class foo

#

the method takes in a string "Meaning of life?" and is supposed to return 42

turbid falcon
#

plz is there somebody to help me with colors 😄

#

(colored text in console)

#

print(f'this in {Fore.GREEN}color{Style.RESET_ALL}!')
#

i tried this from stack overflow and many more

#

but it still keeps printing something like

#

this in ?[32mcolor?[0m!

#

:<

stoic furnace
turbid falcon
#

um

twin pier
#

how can i automatically extract request headers using remote debugging?

twin pier
#

hmmm maybe pychrome can do it...

sharp cliff
#

Hey guys, do you know whether it's possible to fetch rendered images from Selenium?

#

Like if I want to get all images from a website - do I need to download them all using the URLs which I fetch from the website or is there any other option I'm missing

cerulean fjord
#

i know how to test, and using unittest and pytest, but what is automated testing??

twin pier
#

@cerulean fjord using any kind of automation (for example, python code) to test a system IS automated testing.

#

nevermind

proud nebula
acoustic kraken
#

Has anyone here ever encountered an issue where a Selenium script will run fine until running it in headless mode? When I run in headless mode is just hangs on some js file. Please @ me if you reply

digital grotto
#

This channel is specifically for testing Python libraries and applications through testing frameworks such as pytest and the like, not exactly for Selenium, which a lot of online resources seem to brand as an automated testing framework, causing understandable confusion especially with beginners. If you do require help with Selenium though, feel free to use our help channel system. See #❓|how-to-get-help for more information.

#

@acoustic kraken See this

acoustic kraken
#

I am using it with Locust if that counts 😛

digital grotto
#

Ah, yeah that's fair game

pale jungle
#

Hello! i am using selenium and i want to have the results(result that the test has passed or the error in case that it hasnt passed) of every test and write it down in a .csv.

In other words, i call all the tests, see the .csv that everything is okay and repeat.

Is it what i am thinking possible or is there another effective way?

hollow basin
velvet yoke
#

Hello, everyone.
Could you help me with pytest selenium?

hollow basin
velvet yoke
#

OK

#
import pytest
from flask import request, url_for
from fpocalc import create_app
import requests
import selenium

@pytest.mark.usefixtures('live_server')
def test_server_is_up_and_running(client):
    res = urllib2.urlopen(url_for('index', _external=True))
    print("---------", res.code)
    assert b'OK' in res.read()
    assert res.code == 200

hollow basin
velvet yoke
#

live_serve is not working on pytest flask

viral cliff
#

Hello ppl i am new to this server i want to hangout with anyone who likes to code or wants help we could go on a zoom meeting and work on projects just contact me 🙂

proper wind
#

Not sure if this is the right channel for that

hollow basin
#

I'm not sure if I should recomment the stdlib unittest to beginners or pytest. I prefer pytest, but you have to know pip and fixtures can be pretty confusing. unittest doesn't have any handwavy meta stuff but is just very not Pythonic.

opal anvil
#

hmm

#

i think people generally manage to deal with handwavy meta stuff

#

since that's kind of what learning programming is like

rotund ivy
#

Hi I'm new to this server, I'm trying to test my flask app but struggling with pytest and testing in general, does anyone have advice or links to tutorials?

hollow basin
#

If I'm making a library, should the tests be in a directory in the top-level directory for the whole repository, or should they be in a directory within the main source directory?

torn mirage
#

afaik they're usually at the top level dir

tiny widget
#

I’d say it depends if you want to distribute the test with your library or not

torn ermine
golden oxide
#

Can we Solve CAPTCHA with Web automation??

#

Anyone aware of this guys??

low pagoda
golden oxide
#

You mean combination of letters and numbers?

low pagoda
golden oxide
#

Thanks @low pagoda

low pagoda
#

Your welcome

vivid isle
#

In PyCharm, how do I get rid of the bottom % stuff after the process finishes?

golden oxide
#

Can't I use SELECTION(selenium function) if listbox looks like this " unselectable= on" ???

#

I tried different strategy, I could not select any items in the list

golden oxide
#

Any idea guys??

dawn vortex
golden oxide
#

@dawn vortex thanks lot 👍

oblique crater
# hollow basin If I'm making a library, should the tests be in a directory in the top-level dir...

Depends, one way is to have them under tests directory, just that there are different tests not just UTs so I like them separated. Another approach would be to have UTs along package they test, which is logical and fine, but it makes tests discovery more of a pain since frankly pytest works best when tests are in tests directory or subdirectory, there's also a twist of whether to put all into package and tests as simple dir without __init__.py it's about how it finds the root of project, it's documented, but frankly I don't like testing framework to dictate how my tests and project has to lay on modules and packages - maybe that's just me

cyan musk
#

pretty cool

#

you got the github page for tht

#

that

#

that*

tall jacinth
#

is there anyway to make nested @pytest.mark.parametrize only output certain combinations for certain parametrizations. for example: (1, 2) followed by ((5, 6), (7, 8)) should give

#
1 5
1 6
2 7 
2 8
#

whereas right now (assuming that the second specification was flattened, I'd get):

#
1 5
1 6 
1 7 
1 8
2 5
2 6
2 7 
2 8
foggy kiln
misty roost
#

?charinfo ✅

languid sentinel
#

I have just written my first test

#

using pytest

#

and it fail at import 😦

low pagoda
silent mist
languid sentinel
# low pagoda Can you paste the error?

I was so focused on import error that I didn't read Pytest documentation good enough, I should run test using python3 -m pytest so sys path is included. This resolved issue.

south scaffold
#

Hey everyone!

#

I'm trying to run pytest on github actions. and my test keeps failing, saying:

  shell: /bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.9.1/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.9.1/x64/lib
ERROR: file or directory not found: /tests/test_sprint2.py```
#

the /tests/.py file do exist

#

i've tried just running python -m pytest test_sprint2.py but I get the same error

languid sentinel
#

If I yield object in fixture can I pick it in other test?

kind meadow
south scaffold
#

i figured it out,

#

I actually didn't need to even set the path. it's already there. so i just run python -m pytest and it' worked

kind meadow
#

That's probably auto discovery of tests

digital forge
sterile forge
#

I'm trying to tests a Python application that mounts and unmounts filesystems. I'm using the udisks2 dbus service. I'd like to test my application but since the root user is required, I'm wondering what the best approach would be? Any help or hints? Thx!

low pagoda
trim oyster
#

does pytest page object model on HomePage.py is normal have 500++ line of code? since each element have method visible, get text and click?

dawn fulcrum
#

could anyone point me to some resources for learning unit-testing?

cinder knoll
# dawn fulcrum could anyone point me to some resources for learning unit-testing?

Corey Schafer's unit test videos are a solid if you are just starting out.
https://www.youtube.com/c/Coreyms/videos

dawn fulcrum
cinder knoll
rustic saddle
#

I think this is the best channel to put this question out. I am trying to scrape some reviews from a website for a school project, but I keep getting StaleElementReferenceException.

Using selenium for webscraping

#
 right_container = review.find_element_by_class_name("c-review-block__right")


    review_date_box = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, "c-review-block__row")))

    review_date = right_container.find_element_by_class_name("c-review-block__date").text

    print(review_date)```
dawn fulcrum
#

unless u got unit-testing

velvet glacier
#

yeah I was gunna say that

buoyant siren
#

What are terminals used for in programming in general I see many people in videos using it however whats its main use in a professional programming situation

#

idk just why do many programmers with and without jobs uise it

#

I see they also test stuff with it

lucid drift
#

Terminals are used for many things, like accessing files, checking network info, editing code in the case you use vim, communicating with servers, and if you use a terminal on a linux based OS you can basically make anything you would make on a graphical interface from just a terminal, many people find more efficient and practical using a terminal once they get familiar with the commands and how do they work.

proper wind
buoyant siren
#

where can i learn about the terminal and how it works

#

Ik there are reocurces everywhere

#

do you know any good recoruces?

fluid quiver
#

this channel is probably not the best place to be talking about terminals. i'm not sure if there's an appropriate topical channel, maybe try asking in #python-discussion or a help channel?

wide carbon
#

!paste

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.pydis.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.

latent snow
#

Guys, can someone please explain to me why this is happening?
I run Django in docker-compose, it runs fine. But when I run tests, they remain from an older version.

#

This is the comparison. Top is my current code but tests (below) are being run on the old one.

#

(I moved a class function to be a signal)

#

I deleted redis and centrifugo containers (I know, shame on me) and it started working again. Can I make them start fresh every time?

#

Looks like it's redis.

brazen flare
#

Hello all I'm trying to unit test a django rest API

#

I wrote a test to test models

#

But when I run python manage.py test, I get

System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
#

Let me list my project structure

#
Project dir
|
- MyProject
- myApp
  - tests
    |
    - __init.py__ 
    - test_models.py
-manage.py
river pilot
#

Your file should be __init__.py, not __init.py__

#

and you should share what's in test_models.py

thorny sleet
#

So how important would you guys say unit tests are in a job

#

would you use unit tests for games?

brazen flare
river pilot
brazen flare
# river pilot what's in test_models.py?
from django.test import TestCase
from ..models import Tour

class TourTest(TestCase):
    """ Test module for Tour model """

    def setUp(self):
        Tour.objects.create(
            company='Trip Advisor Co', 
            title='Western European Tour', 
            description='This travel tour is to traverse Western Europe starting from the Uk', 
            date='April 2021 to May 2021',
            price= 499.99
        )

        Tour.objects.create(
            company='Digital Nomad Co', 
            title='South East Asian Tour', 
            description='This travel tour is to traverse Asian nations', 
            date='June 2021 to July 2021',
            price= 599.99
        )

    def test_tour_companies(self):
        company_advisor = Tour.objects.get(name='Trip Advisor Co')
        company_digital = Tour.objects.get(name='Digital Nomad Co')
        self.assertEqual(
            company_advisor.title, "Western European Tour")
        self.assertEqual(
            company_digital.title, "South East Asian Tour")
#

This is my project structure -

#

@river pilot I did this just now -

#
python manage.py test indieTourApis
#

I got this error instead

#
 "INSTALLED_APPS." % (module, name)
RuntimeError: Model class indieco.models.Tour doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
#

In my installed apps -

#
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'indieTourApis.indieco.apps.IndiecoConfig',
    'corsheaders',
]
brazen flare
#

@river pilot I was able to resolve it

river pilot
#

ah good

brazen flare
#

In my test_models.py, the import was the issue

#

Changed
from ..models import Tour
to
from indieTourApis.indieco.models import Tour

icy summit
#

i wanna try out coverage.py but im using poetry for my dependencies

#

i cant find anywhere how to use the two together

#

sequencer is where the source code is

#

i use poetry run pytest to run all my tests

#

any ideas?

river pilot
river pilot
icy summit
#

That doesnt work, show missing imports

river pilot
#

can you show the output?

#

your other option is to use the pytest-cov plugin

icy summit
#

and with poetry run coverage -m pytest i get

no such option: -m
Use 'coverage help' for help.
Full documentation is at https://coverage.readthedocs.io
river pilot
#

oh sorry: poetry run coverage run -m pytest

icy summit
#

yea that gives me that red error

river pilot
#

ok, that's the classic pytest importing thing, and i forget how to solve that

icy summit
#

ok i got pytest to run but this is just sad, struggling with a package

#

beaten by bits and pixels

river pilot
#

i know the frustration

#

it will be worth it to get it going

icy summit
#

i also struggled with finding help for this

#

does no one else have this issue with poetry and coverage

river pilot
#

the import problems are about pytest, not coverage, i think

frozen zenith
#

hey , I m trying testing for the first time. I am trying to test custom fields in django . But whenever I try to run python manage.py test i get this. Please help if possible

#

for test_fields.py i pass some value as kwargs

limber willow
#
    # Your code here
   mess = [i for i in range(92,123)]
   reverse = [j for j in range(122,96,-1)]
   
   n =0
   encrypt = {mess[n]:reverse[n] for n in range(len(reverse))}
   out = []
   for i in x:
       if encrypt.has_key(ord(i)):
           out.append(chr(encrypt[ord(i)]))
       else: 
           out.append(i)
   return ''.join(out)

message = "Yvzs! I xzm'g yvorvev Lzmxv olhg srh qly zg gsv xlolmb!!"
message = solution(message)
print(message)```

I am getting error in this code that has_key is not an attribute

Error:
```PS E:\Python> python -u "e:\Python\I-love-lance-janice.py"
Traceback (most recent call last):
  File "e:\Python\I-love-lance-janice.py", line 17, in <module>
    message = solution(message)
  File "e:\Python\I-love-lance-janice.py", line 10, in solution
    if encrypt.has_key(ord(i)):
AttributeError: 'dict' object has no attribute 'has_key'```
ember maple
#

@limber willow has_key is id no longer available in modern python - i think your code is for python2

#

@celest ledge also it seems like code is a bit wrong, as mess and reverse have different lengths

#

@limber willow am i understanding it correct that the intent of the codec was to map from the alphabeth to the reversed alphabeth for both uper and lower case?

limber willow
ember maple
#

i would recommend to create a reverse mapping of the ascii alphabets correctly then

limber willow
#

ok

ember maple
#

mapping = dict(itertools.chain(zip(string.ascii_lowercase, reversed(string.ascii_lowercase)), zip(string.ascii_uppercase, reversed(string.ascii_uppercase))))

#

then you can do a translate_map = str.maketrans(mapping)

#

and viola ```

"abc!".translate(translate_map)
'zyx!'

#

you may need to add imports for both itertools and string

limber willow
limber willow
ember maple
#

i see, in that case i suggest to do some tutorials and play around

#

in any case the code you posted is for python 2 and not for python 3

proper wind
#

hello. I am a beginner at learning python, can someone explain the mistake i did in line 4?

#

ping me when answering please

ember maple
#

@proper wind there is the : at the end missing

#

and i think you meant 3 == ...

proper wind
#

so

  if 3 == one + two:
```?
#

oh

ember maple
#

also there is wrong indentation of the of statement

#

i strongly recommend tutorials and editors with error highlighting

proper wind
#

alright thanks

#

ok it works now ty

hollow basin
#

woah a pytest dev lemon_hearteyes

umbral sparrow
#

I was trying to set up code coverage for a project which uses Cython. I used cython plugin as specified https://docs.cython.org/en/latest/src/tutorial/profiling_tutorial.html#enabling-coverage-analysis. Everything works, but I have many catching for MemoryError where it is not possible to test it. How to exclude it from coverage? I tried pragma: no cover but it didn't seem to work, but it works with .py files. Is there any other way I can exclude them like regex or something? Any help appreciated.

ember maple
#

@hollow basin im here since a while, time to do something

ember maple
hollow basin
# ember maple Auto complete fail

Ah. Well I discovered pytest recently (I had been using the stdlib unittest) and I actually enjoy writing tests (sometimes) now. Thanks for your contribution!

ember maple
#

We are happy when users are

fierce flare
ember maple
#

hmm, i don't like that one at all, no higher level modeling

fierce flare
ember maple
fierce flare
ember maple
fierce flare
ember maple
#

well, working with more complex forms and having to be really sure about which exact data element to select, which sub element to use, how to verify where you actually are, those broad selectors dis a very bad job at consistently selecting the right data over time

#

in particular when updates to libs of frontend parts change the element structure of react components, the stuff you linked is prone to utter breakage

fierce flare
#

Only if changing the id of elements. The id is used whenever possible, guaranteeing selector uniqueness.

ember maple
#

the ids have to dynamically compute

#

working with tables, multiple data vies that replace, tabs, and everything is nested somewhere

fierce flare
#

You can choose your own selectors. Maybe the id isn’t one you want to use in your case.

ember maple
#

@fierce flare how would you create an abstraction with that that allows to view the data in a table as objects selecting the rows correctly and fetching data ids of objects off those rows and their data elements

fierce flare
#

(Uses bs4 / beautifulsoup)

fierce flare
#

Here’s the nth_child_test, which works and finds rows and highlights them.

ember maple
#

i don't think we talk the same language

fierce flare
#

I’m not sure why we’re arguing. SeleniumBase expands the pytest ecosystem, and is the most popular pytest plugin in terms of GitHub Stars (over 2000 now). We’re supposed to be on the same side.

ember maple
#

again, my point was that SeleniumBase lacks higher level modeling, all code you showed me demonstrated that

fierce flare
#

I’d say it does if used correctly, and I’d be happy to show you over a screen share if you’re ever curious. Lots of companies using SeleniumBase for all of its advanced features.

ember maple
#

@fierce flare all examples you showed me don't even have the slightest hint of such a structural abstraction that handles data modeling in a declarative way, so until there is examples that demo such a capability i doubt that a screen share will be well spend time

proper wind
#

nerd fight woooo

boreal prism
#

ooh

#

nerd fight

#

haha

#

yall going at it huh?

#

Ok imma sleep

modest quartz
#

My new article is out!

This time I want to discuss a pretty useful idea for library authors and their users: there are better ways to test your code!

I give three examples (in #python, but the idea itself applies to almost any language) of how user projects can be self-tested without actually writing any real test cases by the end-user. One is hypothetical about django and two examples are real and working: featuring deal and dry-python/returns.

Short example with deal:

import deal

@deal.pre(lambda a, b: a >= 0 and b >= 0)
@deal.raises(ZeroDivisionError)  # this function can raise if `b=0`, it is ok
def div(a: int, b: int) -> float:
    if a > 50:  # Custom, in real life this would be a bug in our logic:
        raise Exception('Oh no! Bug happened!')
    return a / b```

This bug can be automatically found by writing a single line of test code: `test_div = deal.cases(div)`. As easy as it gets!

From this article you will learn:
-How to use property-based testing on the next level
- How a simple decorator `@deal.pre(lambda a, b: a >= 0 and b >= 0)` can help you to generate hundreds of test cases with almost no effort
- What "Monad laws as values" is all about and how `dry-python/returns` helps its users to build their own monads

I really like this idea! And I would appreciate your feedback on it.
Link: https://sobolevn.me/2021/02/make-tests-a-part-of-your-app
hollow basin
ember maple
# proper wind nerd fight woooo

I don't think so, but there is a misunderstanding there, we will eventually get that communicated in a way that works for both

queen raft
#

So this may be the wrong channel, but it's as close as I could get it. I have a pytest question as I'm using python/pytest for selenium UI tests.
I'm using PyCharm and it adds __init__.py files for every package I create. VS Code does not do this. Is there a way to have PyCharm not do this? I'm using Python v 3.9

kind meadow
#

I don't know, but you generally want to have those files in each package.

#

Without it, you're creating an implicit namespace package.

ember maple
queen raft
#

ah good to know. I had a dev tell me not to check those init files in and that they were unnecessary 😦 but maybe that was for unittest and not pytest

ember maple
queen raft
#

Thank you for your service 🙇‍♀️

ember maple
glacial ivy
#

I am using robotframework for testing

#

any one experienced with it

proper wind
#

I had a short stint with it at my company when designing end to end testing for a new product. We dropped it in then end as it caused more problens than it solved. I would much rather work in python directly than in some weird pseudo business friendly language.

hollow basin
#

Is there a way to have pytest mark a test as skipped if NotImplementedError is raised during the execution of the test?

ember maple
hollow basin
ember maple
hollow basin
#

Sounds good!

ember maple
#

that way its also better integrated, xfails are expected failures - you could even give each method a nice text with that

#

and if you want to separate things a bit more, its entirely valid to add a pytest hook to interpret your own not implemented errors as xfails

#

however in that case i would suggest to make a own subclass of the error to ensure its hitting the correct exception

hollow basin
#

Was the name xfail a programming convention before it was added to pytest? Personally I would not have been able to guess that the x means "expected".

ember maple
#

gimme as second, since its been over a decade and i don't remember

#

xfail was part of pytest even before the concept of marks existed, to get the full history i may have to dig into the pypy repository ^^

#

hmm, its haunting to see commits like "Python2.3 has no sorted() yet" ^^

ember maple
#

nope, all i can find of its introduction is somewhere in a svn branch from 2009

hollow basin
ember maple
hollow basin
slim prairie
#

k

shut hedge
#

If you're interested in testing and pytest, there's an open voice chat today (starting right now) with @ember maple in the pytest server: https://discord.gg/56fpCHWf

proper wind
#

fam anyone uses robotframework work?

#

i need some help

#

or 5g wont work no more

#

i have a params.py file full of variables like
a=1
b=2

i have a robot file that will use a function with those variables

#

when i import them in robot like

*** Settings ***
Variables | params.py
#

i get the variables literally as
${a} ${b}

#

but i want to pass all these vars at once in my function and refer to tham as say
params.a
params.b

tall jacinth
#

is it possible to have pytest-cov /coverage take into account dict lookups as part of coverage

#
c = {
   "a": 1,
   "b": 2
}

d = c["a"]
#

this code has 100% coverage, but I want the "b": 2 to be uncovered. If I used if/elif instead of dict lookup it would be uncovered, so how can I achieve that?

finite abyss
#

Should I learn pytest or just unittest?

fiery arrow
#

Maybe @river pilot can answer lemon_ping

river pilot
#

@tall jacinth we don't have a way to measure "data coverage"

#

i suppose you could wrap the dict in a proxy that noted which keys had been accessed.

river pilot
tall jacinth
#

I ended up just converting it to bunch of ifs that each return in their own branch. Not as concise as dict lookup, but lets coverage track which keys are indexed. I'm imagining once py 3.10 hits and we get match statement, I could make this more concise

#

thanks for help guys

icy locust
tall jacinth
#

ah looks like I can also do this:

#
def get_1():
    return 1

def get_2():
    return 2

c = {
    "a": get_1,
    "b": get_2
}
#

and after doing c[...](), coverage can track which function is called. Will try reading up on how coverage works so I can better understand these examples

tall jacinth
#

@river pilot would it be theoretically possible for coverage to track my first example? not from viewpoint of feature request, but more out of curiosity. I'm checking out how sys.settrace works, and saw that from the frame passed to the tracer, frame.code.co_varnames gives argument names, from which we can get the argument value through frame.f_locals.

#

and then assuming we had a way to indicate to coverage py that we want it to consider 100% coverage of a particular function if it's hit for all the possible arguments we define instead of just once, it could track __getitem__ calls using the above frame attributes (or whatever their equivalent in C is)?

#

if this is possible, I think that the complexity in telling coverage.py what arguments we want to evaluate coverage against makes this as an actual feature veryyy impractical. but taking this another step, function arguments being Enums or typing.Literals (would assume that program type hints were statically verified through) could generalize the above, excluding the special-case scenarios like literal dict's declared inline

frosty raven
#

,

dreamy nest
#

Hi There! What's your recommended testing framework or module with django?
[Django Test, Unittest, pytest]

ember maple
pale pelican
river pilot
strange crown
#

Hello, I am studying mocks for a project and I am trying to figure out how can I mock/patch a class function's behaviour and test a function of the same class. Here is a very simple snippet of code to replicate this problem without using unittest yet.

from unittest.mock import patch

class TestClass(object):

  def __init__(self):
    self.day = None

  def what_day_today(self):
    return self.day

  def ask_day(self):
    return self.what_day_today()  # proper function call


@patch.object(TestClass, 'what_day_today')
def test_ask_day(mock_what_day_today):
  test_class = TestClass()
  mock_what_day_today.return_value = IOError
  print(test_class.ask_day())


if __name__ == '__main__':
  test_ask_day()
#

This prints : <MagicMock name='what_day_today' id='140552585487080'>
Where I just want return the IOError

river pilot
#

You aren't calling what_day_today: you need parentheses.

strange crown
strange crown
#

Follow up question. A real one this time. Let's now try to test the behaviour by mocking/patching a class attribute during test. I am doing something wrong, what is it ?

from unittest.mock import patch
import datetime

class TestClass(object):

  def __init__(self):
    self.day = None

  def what_day_today(self):
    return self.day

  def ask_day(self):
    return self.what_day_today()

@patch.object(TestClass, 'day',  create=True, return_value=datetime.date(2021, 3, 11))
def test_ask_day(mock_day): 
  print(TestClass().ask_day()) # something is wrong here


if __name__ == '__main__':
  test_ask_day()  # prints None, but it must print datetime.date(2021, 3, 11))
strange crown
#

Answer: use PropertyMock

from unittest.mock import patch, PropertyMock
import datetime

class TestClass(object):

  def __init__(self):
    self.day = None

  def what_day_today(self):
    return self.day

  def ask_day(self):
    return self.what_day_today()

@patch.object(TestClass, 'day',  create=True, new_callable=PropertyMock, return_value=datetime.date(2021, 3, 11))
def test_ask_day(mock_day):
  print(TestClass().ask_day())

if __name__ == '__main__':
  test_ask_day()
river pilot
#

be careful with mocks. It's easy to go overboard.

ember maple
#

I prefer to find ways to be without mocks, but sometimes it's practical to use them

river pilot
#

@strange crown I'm curious what your :man_shrugging: reaction means? 🙂

strange crown
# river pilot <@!373424660047462411> I'm curious what your `:man_shrugging:` reaction means? �...

Mocks are new too me, I don't understand them yet. So I don't know how I can get overboard. @ember maple I have been told from my supervisor to start using them in my projects. I am a Data Scientist with computer science background but I am not a software developer. I design and train ML and I deploy them to production. I have only used basic unit and integration tests before. I ll try into introduce mocks to test modules during development.

tall jacinth
#

In general you want to use mocks when the real thing is either external and can't be tested (like sending out an email with a provider) or it would take too long to test (like starting up a temporary database to see if the SQL statements check out)

river pilot
#

(or it's unpredictable in some way)

ember maple
tall jacinth
#

I agree, I think within ML there's little opportunity to mock. But mocking the ML itself might make sense. For example, our ML model in prod runs inference on GPU. So our unit tests use a mock for the model which returns a dummy Tensor (so unit tests can be run on cpu locally), and integration tests will be run only in CI which uses GPU

strange crown
strange crown
late loom
#

hello! just looking for a testing recommendation for Django. Should I use pytest or unittest?

river pilot
#

pytest

fierce flare
late loom
#

awesome, thanks guys - I settled for django-pytest. I've already found some bugs that I'm working on 🙂

hollow basin
#

Does pytest have a context manager where, if any assertions within that context fail, it will still execute the rest of the statements in that test function? I know there's the parameterize decorator but I'm just wondering if pytest also has a feature like that.

river pilot
#

i don't think a context manager can do that.

hollow basin
#

if the context manager suppresses only the first failed assertion in the context, can't context managers do that?

tall jacinth
hollow basin
tall jacinth
#

Another nifty thing you can do if applicable is assert (a, b) == (c, d) instead of separate assert statements so you get values of b and d even if a and c are unequal.

ember maple
hollow basin
ember maple
autumn lagoon
#

does someone have example code of mocking user input to a function with multiple iterations of input? been trying to figure it out with patch decoration alongside side_effects but a good example would help

#

keep finding lots of deprecated examples in my searches so far

ember maple
autumn lagoon
#

@ember maple I see, so this wouldn't be an optimal test build:

badIPs = ['256.255.255.255', '-1.55.25.155', 'a', '192168192168']
...
    @patch('builtins.input', side_effect=badIPs)
    def test_input_validation(self, mock_input):
        for i in badIPs:
            result = getIP()
            self.assertRaises(ValueError)

Forgive me if it's sloppy, still trying to wrap my head around the different pieces with this

#

I am trying to abstract out the bad test values so they are easy to alter

ember maple
autumn lagoon
#

okay i was seeing the list of repeated assertions and felt like that violated DRY. so it's better practice just to list them out below and iterate through manually?

ember maple
#

No, tests against different values match loops or parameterized tests

#

But test just the parts that parse, not the surrounding ux stuff

autumn lagoon
#

i see what you're saying now

#

do you have recommended best practice examples beyond the unittest docs

#

i think more exposure would help me grasp it better, but all the different variations are muddling it up for me

#

particularly with parsing out functions with mock

ember maple
ripe marlin
#

I love pytest. until I get to network or UI code and realise I haven't learned how or where to put test doubles.

proper wind
#

Anyone around familiar with running pytest in parallel?

#

I have a bunch of tests I want to run. I have my tests set up a database and run one at a time.

#

I want to run them in parallel, but it seems like pytest-parallel and xdist will both build containers no matter what

#

I need just one

#

I have getting the container up and running as a fixture

#

I'm wondering if someone knows how to deal with that kind of scenario.

split field
rapid talon
#

Hi, I'm new to unittest and trying to test the below functions
`
missions_details_list = []

def get_mission_details(name):
for i in missions_details_list:
if name == i['name']:
return i
return None

def update_mission_status(name, new_status):
for i in missions_details_list:
if name == i['name']:
i['status'] = new_status
return i
return None
`
I'm tring to test the above functions and the problem is mission_details_list gets updated from a different python file. Is there a way I can populate mission_details_list with some temporary data and then test the functions?

low pagoda
rapid talon
#

yeah I know, brother.

#

It's just something I'm tinkering with

#

I wanna know if I can test it

#

It always seems to return None which is obvious cuz the list is empty

low pagoda
rapid talon
#

it doesn't help

#

it's looking at the list and it's finding it empty

#

The tests pass if I manually add some data to the list and then test the function

low pagoda
#

Show me your tests

rapid talon
#

When I don't put the data in manually. Also, everything has been imported in the test file

low pagoda
rapid talon
#

This is when I pass it manually. As you can see, both the test cases pass

rapid talon
#

I added:
`
def setUp(self):
self.missions_details_list = [
{
"name": "GG",
"status": "Assigned",
"avengers": "Thor",
"details": "Good Game"
}
]

`

#

Still returns none

#

and then if I add:
def test_len(self): self.assertEqual(len(self.missions_details_list), 1)

this test case passes

low pagoda
snow topaz
#

Hi, is this channel about unit testing only, or about other types of testing as well (e.g. load testing)?

low pagoda
#

Try to import missions_details_list in your setUp and fill with data above

low pagoda
#

I think that is okay to ask about load testing here

#

However I am not from the crew

snow topaz
#

Thank you.

strange crown
#

I would like to mock a request response so it returns a callable json object within the response but I don't know how. Not really of mock/test question but this will be a test afterwards. I could use flask.Response maybe but I ll prefer not to *

  def test_response():
    def res():
      r = requests.Response()
      r.json = {"key_0": "values_0"}  # this doe not work
      print()
      return r

    return res().json() == {"key_0": "values_0"}  # this must be true, code to be tested calls r.json()
river pilot
#

there are specialized packages for mocking requests responses.

strange crown
strange crown
#

By using requests-mock this is how it should look like. Again this only a POC.

import requests_mock
import requests
@requests_mock.mock()
def test_response(m):

  payload = {"key_0": "values_0"}
  m.register_uri('GET', 'mock://test.com', json=payload)

  session = requests.Session()
  adapter = requests_mock.Adapter()
  session.mount('mock', adapter)
  resp = session.get('mock://test.com')
  return resp.json()

print({"key_0": "values_0"} == test_response())
sharp rose
#

What do you guys think about my testing library? I am looking for feedback to improve the project 🙂

ember maple
mystic shore
#

Hi everyone! I'm looking for a way to automatically post a pull request comment with failing test information whenever my tests fail on CI. Ideally a comment that looks nice, maybe with failure logs in collapsed groups, some colors, something that's really easy on the eyes.

Using pytest as my test runner, and running the tests in a matrix of 8 workers with pytest-split.

Anyone familiar with something that would do this, or do I have to build it myself?

kind meadow
mystic shore
#

hmm, that does look promising.

mystic shore
#

thanks @kind meadow, I think that puts me on the right track. I might just use the one you linked, actually.

kind meadow
#

You're welcome. The annotations are close enough to leaving a comment.

#

It's also written in python so if it came down to it, you could fork it

mystic shore
#

huh. cool.

tiny widget
#

That’s a cool action

#

Alternatively you could have a step that will only run if the tests failed that will parse the results and send a message using the github token

mystic shore
#

that's what I'm looking for, just without having to reinvent a wheel that definitely already exists.

tiny widget
#

Yep yep

shut hedge
outer flicker
#

hellothere

cloud warren
#

Hi guys can some one find limitations in my program , I need it for a school project

#

Dm and I will send u the code

proper wind
#

Does anyone have any ideas for how one might leverage AWS server less to run integration tests? I'm trying to switch our executor over from Jenkins to Lambda, since there's 15 min limit, I was thinking of perhaps a lambda instance per test suite, but I can't think of how reporting would work, since I was to keep these all outputting the same report html file

#

Anyone have any thoughts on ways I could make our pytest pipeline serverless? I'm by no means new to python but I am new to AWS

ember maple
#

@proper wind IMHO serverless and actual testrunners are currently on opposite ends of a certain spectrum, an actual test run is not something i'd want to put on a lamabda/serverless setup as is

they are more like jobs to run/configure/contextualize and less like functions to transform

ember maple
proper wind
# ember maple <@456226577798135808> IMHO serverless and actual testrunners are currently on op...

Thanks for the reply!

Our companies current approach is the usual, multiple Jenkins executors kicking off jobs. Our test lead wanted to investigate being able to abstract this execution to the cloud to not necessitate executors and have them be ephemeral and scale easily

Just to clarify you're saying this is better scaled from a k8 run or something similar than trying to stick it in AWS? The idea had been each individual integration test case (I know I put this in #unit-testing) run as a lambda

ember maple
proper wind
#

Awesome. That's all I needed for the pushback on this action item. I'll convey that to him

ember maple
proper wind
#

I've kinda gathered the same from doing this spike work

#

Right. Except the scaling executors part

#

Afaik it's static through Jenkins no?

#

Resources as well, if we have heavy test run load

ember maple
#

Jenkins can deal with k8 dynamics these days, same goes for k8 targeted ci tools

proper wind
#

I'll propose that as an alternative. Thanks for taking time out of your day and your work!

I'll join the open chatter call if there's any followup lingering Questions

ember maple
#

Please note that my coworkers manage the actual ci deployment on openshift, so part of the info I have is second hand, maybe someone with first hand will join as well

ember maple
#

the pytest open chatter is on ^^

open quiver
ruby copper
#

I am starting to get into learning how to unit test my work properly

#

as a really basic overview, a unit test would be like creating an example program that uses a method/class you wrote and confirms that it works properly with different inputs

#

or is that too basic of a test?

low pagoda
# ruby copper as a really basic overview, a unit test would be like creating an example progra...

Let say you have class like this

class Calculator:
  def sum(a, b):
    return a + b
 
  def mul(a, b):
    return a * b

Now you can create some test cases like sum of 3 and 4 should give 7 or mul of 8 and 0 should give 0.

def test_sum():
  calculator = Calculator()
  assert calculator.sum(3, 4) == 7

def test_mul():
  calculator = Calculator()
  assert calculator.mul(8, 0) == 0

There are a lot of improvements which can be applied here (like one instance Calculator or parameterized tests) however it's simple example what unit tests are.

digital grotto
vague chasm
#

Hi, does anybody know how to test method that converts from json to xml?

kind meadow
#

Give it an input, run the function, get the output, assert the output is what you expect it to be

#

And do that a bunch of times for inputs that cover different cases

vague chasm
#

thank you

fervent obsidian
#

especially the var local and name, i don't know if they where work on os.system commands

timber rock
#

How do we get the test result in a pytest fixture of a test that used? I want to delete some artifcats conditionally on test passing after the yield statement (teardown part)

#

other way works too

#

i.e. if test fail copy from tmp directory to a save directory

hollow basin
#

@timber rock I'm not following. Can you share what you've written so far and be more specific about what you want it to test?

timber rock
#

dont want it to test just want to delete the artifacts if test is successful

#
@pytest.fixture()
def myfix():
  make temp dir
  do stufff
  yield stufff
  if test failed:
    copy_stuff_to_not_temp_directory_to_look_at

def test_fun(myfix):
  use stufff```
hollow basin
#

so if the test failed, you want to log information about what happened during that test?

timber rock
#

yeah some json files are a byproduct of the test

#

the pytest temp directory only keep the last 4 runs

#

so just going to the temp directory doesnt work too well if i want to do it later

hollow basin
#

I might have time in two hours or so to look into it more, but as you might know, pytest will catch everything from stdout and include it in the report if a test fails

#

so if they're not too horribly long you can just print them

timber rock
#

yeah im talking about artifacts, not stdout

#

theres 60k lines ish 🙂

#

per json

#

for 200 tests lol

hollow basin
#

I see. I'm clearly not the one to be helping you with this.

timber rock
#

thanks for trying

#

stdout was a good idea

hollow basin
#

Hopefully Ronny will grace you soon enough 🥳

night timber
#

Hi everyone

#

Is anyway to perform unitest on python in a easy way without having to go through each method/f

#

I am very beginner so sorry if the question is nonsense

hollow basin
#

however, tests are not magic. A lot of tests consist of calling functions/methods that you wrote with specific inputs and checking that the output is what you expected

#
# main.py
def double_number(number):
    return number * 2

# test.py
def test_double_number():
    assert double_number(3) == 6
    assert double_number(0) == 0
    assert double_number(-2.5) == 5.0
night timber
#

Thanks, as I am learning and I have too many questions about it I need to deliver a school project and after getting a lot of help to develop an app I need to do a unit test

#

But don’t have much time to get it done and was looking for a way to deliver it quickly then have time later to understand

#

Maybe counting around 45 methods /func to create unit tests

#

As still learning someone I know mentioned that it would take me 35 minutes average to each method unit test ( I mean to write it )

hollow basin
night timber
#

Yes I do know

#

There’s somethings at stake anyway and particular reasons

hollow basin
#

what are you trying to test?

night timber
#

A app

#

An

hollow basin
#

What does the app do?

night timber
#

Just flask app

#

Simply add people users profiles to a simple platform intended to help people who are hitchhiking

hollow basin
#

are these being added to a database?

night timber
#

Yes

#

Postgres

hollow basin
#

I'm not experienced with testing databases

gaunt ibex
#

Context: I am using an open source repo as a timing utility. Their decorator failed because it writes the arguments of the function to a log file using the JSON library and my arguments are pandas.DataFrame s which can't be written in this fashion. The repo had no tests.

I added a flag to get around my issue, I also added pytest and built tests to make sure the edge cases were working how I expected. That felt reasonable to me to put into a pull request. It introduced one dependency (everything else in the code base is pure python, i.e. no dependencies), but I think it probably make the code base better.

In my tests, I also want to include pandas.DataFrame objects so that I can test my edge cases, but this introduces a large number of dependencies, which really don't impact anything other than the tests. I am torn about if this is reasonable to do. Add a bunch of dependencies to a relatively clean project so that you can test edge cases that the original author didn't care about. Any thoughts?

proper wind
#

what does boundary/errounous and normal testing mean

#

i made a dice game
but this is my first time doing a testing table so idk what to do

#

anyone?

#

@hollow basin ? sry for the ping but i need an ans quick

#

@timber rock

#

?

hollow basin
#

@proper wind please don't ping random people. While we try to help out around here, it's never guaranteed that someone is available

proper wind
#

Kk

#

sry

#

i did expalin

night timber
#

I understand

bitter wadiBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

exotic depot
#

does anybody know how to make a voting bot for online polls like polldaddy

lucid palm
#

Hello. Do you know good tutorial about TDD in python with real project? Something more than FizzBuzz 🙂

ashen egret
#

What's up, devs? Can I ask here a question about selenium? or should I go to another topical chat? thanks

ashen egret
proper wind
#

@all_jupyter_notebook_users: how do you import a class from a jupyter notebook in an another notebook ?
i tried import_ipynb but it still complains module not found.

proper wind
#

sorry if i am in the wrong channel, i dont know where this is classified

lament geyser
#

@proper wind are you proving enough arguments when you run the file?

proper wind
#

ohhh

#

i had to specify another number

#

thx

proper wind
#

what do i import instead of requests

#

if i remove it entirely, it doesnt wokr

#

work

#

i realised i gotta install it nvm

proper wind
#

how do i install requests

fiery vale
hollow basin
#

!paste

paper crow
#

Hi there, I'm developing a pytest plugin to change the order which tests run to start with the ones that "historically failed more"

#

the way I'm doing initially is saving the test results in a hidden file on pwd, and the plugin read this file to calculate the "ratio" of failure for each test

#

I want to move this solution to a more "professional" one, where I don't save local files, but rather use the local
user's postgres, or something like that

#

anyone wants to "trade some cards" about this?

kind meadow
#

I think it's fine to create a file. You need to remember it, after all. Relying on users to already have a db set up seems naïve.

#

If you want to use a db, why not sqlite

paper crow
#

good point about sqlite

kind meadow
#

This doesn't seem like something that would need a full fledged db though, but I could be proven wrong

#

You can follow what coverage does with its .coverage file

#

I know it's not exactly in the same vein but the point is that it just makes a dotfile in the project's directory.

paper crow
#

that's what I doing right now, a dotfile

#

maybe I'm thinking too far, but what about tests that run in CI/CD pipelines

kind meadow
#

Pipelines usually have cache facilities so let it be the user's responsibility to cache the file and whatever else.

paper crow
#

well, I'm going to need some kind of url to post too in any case

#

I'm thinking of a central repository for the information of "failure ratio" 😅

#

but that would be a cloud service of some kind

#

(something that would "reflect" the reality of tests failures in local environment and in CI/CD too)

kind meadow
#

Aren't there already services that track test run stats from CI?

#

If they have an API you could just pull stats from that honestly

paper crow
#

that's a good point

kind meadow
#

I know Azure Pipelines at least had a way to publish test results and it would aggregate stats. It would even show which tests fails often.

#

That's pipeline-specific though but I've seen similar services used in projects. I just cannot remember the names.

paper crow
#

well, as a plugin I could try to offer "integration" with those services

#

the most common ones, to get started

#

thats good advice, thanks Mark

kind meadow
#

Yeah, if you want to focus on pipeline support rather than local runs

#

You're welcome

paper crow
kind meadow
#

Cool, I'll star it. I think this is actually a good idea - specifically ordering test executing by failure frequency

#

Saves time by letting it fail early

#

Though I wonder how it would deal with parallel execution

paper crow
#

yes, it started as a problem in my former company

#

we had test suites that took >30minutes to run in CI and on the last minutes it would fail

#

becase most of the "sensitive" tests were executed last

paper crow
#

we'd use it in my company

kind meadow
#

Oh, that's great then

random ether
#

Hey guys, is anyone tried unit test using along with behave?

#

do you guys think is it worth to use behave for unit test? or not necessarily ?

low pagoda
fast niche
#

I'm getting into unittesting, and I find it hard to know where to mock stuff. In this example Path.is_dir(path) gets mocked but path.is_dir() does not. What do I need to do to mock path.is_dir()?

from unittest import mock
import unittest
from pathlib import Path


def example(path):
    """:type path: Path"""
    Path.is_dir(path)  # This is mocked
    path.is_dir()  # This is not


class ExampleTestCase(unittest.TestCase):

    @mock.patch('test3.Path')  # This file is called test3.py
    def test_example(self, mock_path):

        example(Path('example/path/to/thing'))

        self.assertEqual(2, mock_path.is_dir.call_count)


if __name__ == '__main__':
    unittest.main()
pearl cliff
#

@fast niche i don't think you can patch something that only exists inside a function. you can only patch things at the top level

#

also in this case you would use mock.patch.object(Path, 'is_dir') because you already have Path in scope

#

mock.patch by itself is used for mocking things that are deep inside other things

#

for example, lets say you had foo.py:

from pathlib import Path

def example(path: Path) -> str
    return 'A' if path.is_dir() else 'B'

and test_foo.py:

import unittest
from unittest.mock import patch

class TestExample(unittest.TestCase):
    @patch('foo.Path.is_dir')
    def test_example(self, mock_path_is_dir):
        example(Path('example/path/to/thing'))
        mock_path_is_dir.assert_called_once_with()
#

that said, i do not recommend writing tests like this in general

#

you end up writing tests that depend on internal implementation details

#

the only time you want to write tests like this are when specifically interacting with the outside world

#

in this case i guess it's ok because you're checking if something is a directory

#

but maybe instead you would want to inject a result and then test different code paths

#
import unittest
from unittest.mock import patch

class TestExample(unittest.TestCase):
    def test_example(self):
        p = Path('example/path/to/thing')
        for val, expected in [(True, 'A'), (False, 'B')]:
            with self.subTest(val=val):
                with patch('foo.Path.is_dir', return_value=val) as mock_path_is_dir:
                    mock_path_is_dir.assert_called_once()
                    self.assertEqual(example(p), expected)
#

or maybe you do both

fast niche
#

Thanks a bunch for answering! I'll have to experiment with this tomorrow!

fast niche
# pearl cliff you end up writing tests that depend on internal implementation details

The example I have given is a bit arbitrary, but not too far from the truth. The program that I'm unittesting creates some binary files, and I obviously don't want to create those files when testing the program.

I'm trying to think about not being dependent on implementation details, but it's hard to create good test cases blobsweatsweary Maybe I am better off refactoring my code so the creation of a file is separated from writing to that file, so I can independently create tests that check if I am writing the right stuff and tests that check that file creation works as intended.

pearl cliff
#

fwiw it might not actually be bad to create a file as long as you can do it in a temporary location specific to your test suite

fast niche
#

Fair enough

pearl cliff
#

theres actually nothing wrong with that unless for some reason you need to deploy and test in an environment where you can't access a filesystem

#

yet another possibility, if you get really clever with patching, is to patch open() to return an io.StringIO that you can treat mostly like a file-like object

#

!d g io.StringIO

bitter wadiBOT
#
class io.StringIO(initial_value='', newline='\n')```
A text stream using an in-memory text buffer. It inherits [`TextIOBase`](#io.TextIOBase "io.TextIOBase").

The text buffer is discarded when the [`close()`](#io.IOBase.close "io.IOBase.close") method is called.

The initial value of the buffer can be set by providing *initial\_value*. If newline translation is enabled, newlines will be encoded as if by [`write()`](#io.TextIOBase.write "io.TextIOBase.write"). The stream is positioned at the start of the buffer.

The *newline* argument works like that of [`TextIOWrapper`](#io.TextIOWrapper "io.TextIOWrapper"), except that when writing output to the stream, if *newline* is `None`, newlines are written as `n` on all platforms.

[`StringIO`](#io.StringIO "io.StringIO") provides this method in addition to those from [`TextIOBase`](#io.TextIOBase "io.TextIOBase") and [`IOBase`](#io.IOBase "io.IOBase"):... [read more](https://docs.python.org/3/library/io.html#io.StringIO)
pearl cliff
#

or in your case io.BytesIO since it's binary

#

but that might not provide enough benefit to be worth the complexity

fast niche
#

huh, that's an interesting solution. Though you may be right that it's probably not bad to just create the file. The reason I decided to patch it is I was thinking about what would happen if the user doesn't have enough storage on their PC, but that does seem pretty unlikely considering the files I'm creating are quite tiny.
In that case would it be a good idea to bundle a correct version of the file with the program so it compares them, or is it better to just store the correct content of the file in my test case?

#

Anyway I really should sleep right now, but thanks for your help!

turbid adder
#

hey , anyone know how to manage proxies on selenium?

pearl cliff
candid egret
turbid adder
random ether
gleaming stream
#

Hi all! I'm currently developing a multithreaded websocket application and were wondering about best practices when it comes to unit testing these types of applications. I suppose no "one size fits all" kind of solution exists, and you always have to adjust for the given application, but I'm interested in hearing how other people tackle these challenges.

One challenge with unit-testing these types of systems are getting the threads properly synchronized. One approach I've currently implemented is to mock the websocket send and recv functions, and insert one input- and one output Queue which I can use to control the flow of execution for the different threads using the websockets. This enables a couple of things: 1) I can make assertions on what the application ultimately sends through the websocket, and 2) I can control what the application receives and then make assertions on how the application responds to a known input. One problem I'm having at the moment is that it feels like the tests becomes a bit too complex.

proper wind
#

How to learn unit testing

gleaming stream
#

¯_(ツ)_/¯

icy locust
# gleaming stream Hi all! I'm currently developing a multithreaded websocket application and were ...

My 2cents: in these cases, its best to use the actual send/recv, since the underlying poll etc will have an impact on concurrency and hence might obfuscate some multi-threading errors. I would in the tests spin up a few threads and then insert a random delay inside some functions - this would prolong the execution and amplify any sync issues. Then just send a large no of requests via this test bench.

turbid adder
#

with selenium can we control mutiple windows in a same time

spiral stirrup
#

Question about unit tests and signals in Django.

I have a signal that soft deletes an instance. When I test it in the shell and through postman, it works exactly as I intend it to.

However, when running unit tests on this section of code, the test fails and shows the instance is not soft deleted.

In the case of unit tests, how do signals function into them? Are the changes the signal makes not saved to the DB right away? Even when adding ‘instance.refresh_from_db()’ the test fails.

fresh blade
spiral stirrup
#

Nope. I have an AbstractSoftDeletion model that’s set up to send a pre_delete signal, sets the deletion_datetime, saves the instance, and then sends a post_delete signal.

#

And my tests just mirror the intended flow of my app, calling all the same methods in the same order.

upbeat chasm
#

hey, how can I stop selenium from showing me this terminal? i already tried

options.add_argument("--log-level=3")
and
options.add_argument("--silent")
low pagoda
cinder tangle
#

Hey Python community, I'm kind of searching for a resource about python-rq with TDD, where worker task is able to make db transactions. Doing all that in Flask

empty socket
#

Hi i need to perfrom some efficiency test, which tool should i use? i;ve tried jmeter but it uses a lot of resources. is there another tool like it where i can specify test scenarios?

hollow basin
hollow basin
hollow basin
lucid plaza
hollow basin
#

did you wanna see my sunglasses? they're huge so that they fit over my already gargantuan regular glasses.

lucid plaza
empty socket
#

and i'm looking for the most reliable tool

#

so far jmeter gave this chart

hollow basin
empty socket
#

and i wonder if it looks 'good'

hollow basin
#

I'm out of my lane here, so I'll defer to someone with relevant experience.

empty socket
#

i mean, shouldn't the time response be getting lower when user count is lower?

#

ok, i'll ask there, thx

kind meadow
#

How can I deal with storing test inputs that are quite large in size (like 100 MB each)? I don't want to make people download gigabytes worth of test data 😦

#

One idea I had was putting them in a git submodule

#

Unfortunately, while possible, it would be fairly tedious for me to recreate test inputs that are at a more reasonable size.

#

Another idea was to include a shell script that would download the test files off a hosting site (since the files are already hosted there anyway)

sly swallow
#

I think the latter is the way to go

low pagoda
kind meadow
#

It's a binary format.

low pagoda
#

Hmm... Do you plan to update those data rarely in the future?

kind meadow
#

Existing data shouldn't need to be changed but new data might be added in case there are new features in the future that will be supported and therefore need to be tested.

#

That would be infrequent though

low pagoda
#

I had similar problem and my colleague told me that storing data is not the best choice and it's better to generate it on the fly

#

All depends on that you are able or not to generate the data relatively easy

kind meadow
#

It's pretty complicated. It seems like it'd be a tremendous effort for me to generate it.

#

It's basically a fuckload of complicated protobuf messages along with some other bits here and there

low pagoda
#

Right, so I totally wouldn't store those data somewhere and download it using a script because this is quite often for me that some external resources doesn't exist for older projects

#

Keeping all things in the same way (example using git repositories) looks okay for me

kind meadow
#

I can create files for testing that are shorter through the same way real world data is created, but it's a bit tedious. More importantly, I'd be concerned that it doesn't accurately represent a real world example, since there lots of different possibilities.

#

Maybe looking at coverage could help me with that though.

#

It's more convenient for me to just throw in 20 examples of real data and call it day, but the file size is unfortunate.

swift bough
#

@kind meadow

TL;DR: Set up a self-hosted runner in GitHub Actions and have it run your heavy tests. Let users/contributors run just the lightweight tests by configuring pytest.ini just so.


You could try to isolate your "heavy" tests (any that operate on that 100MB+ inputs) in their own module, outside of your lightweight tests. Perhaps even explicitly exclude them in your pytest.ini. And don't keep the big files in the repo. That way people can contribute or clone w/o downloading gigs.

But you still need to run those "heavy" tests. You could do this periodically on a local machine, intentionally overriding the pytest.ini excludes at the command line (is this possible? probably...but you get the gist of it.)

You could even set up your own self-hosted test runner, a Raspberry Pi or something. And have all the "heavy" files on it. And use GitHub Actions to run the "heavy" tests on every push or pull request.

It's pretty straightforward to set up a self-hosted runner, but there are some security issues with it. If anyone compromises a workflow that runs a job on your local machine, they could do some harm. So be careful in that regard.

https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners

kind meadow
#

Unfortunately, I don't have the means to self-host anything currently. I will consider it though as someone else in the team plans to upgrade the deployment infra at some point and this could be squeezed in 🤷

#

Why would a self-hosted runner be important here? What would be wrong with letting the provided runners run all those tests?

#

Also, the project is a pretty bad code base that I inherited and the tests are very sparse (and currently broken anyway due to major refactoring, but I'm debating if they're even worth fixing to keep around). Relying on those other tests in most cases wouldn't be very helpful to determining if things work as intended.

#

If I could get some smaller data like I mentioned then I'd be more comfortable with excluding some of the larger dataset sometimes.

maiden pawn
#

unless you are using public rep, but perhaps there are hidden limits as well

#

self-hosted runner has no limits

#

use as long as you wish

#

it is limited only by its configs, which you can regulate

kind meadow
#

I see. It is public. I'm using azure but it's a similar situation I'd guess. I'm not sure how long tests would take with large inputs I haven't tried one in ci yet.

maiden pawn
#

I used to set up self hosted runner for gitlab, it was awesome
one self hosted runner, can be enabled in multi threading mod, and provided enough running power for all my private projects checkers to be run at the same time

kind meadow
#

Don't have the means unfortunately. I'd rather have one less thing to maintain myself anyway. But if it comes down to it I'll consider it

maiden pawn
#

considering that your project is public

#

highly likely you aren't needing one

#

not sure about restrictions though? there is possible time limit for one runner length. I have like... 4 checkers, each taking 1-2 minutes (in public, online, free way)

#

it is free for me to use as much as i want, every commit

kind meadow
#

It depends on how many files I'll need to get good coverage. Don't know yet. I also don't know how fast a single run would be in the CI environment.

#

I do imagine it to be like 5 minutes at most though

maiden pawn
#

time can be additionally decreased by cache usage if necessary

#

to be not installing all dependencies from scratch each time

#

also, choosing good... docker base image is important

#

some of them, have already all dependencies installed, which speeds up runs too

#

I usually google 'docker NameOfFramework/Language', to find the right image

kind meadow
#

Yeah I'm pretty familiar with that stuff. Those techniques are used for python discord projects

crude geyser
#

hello guys, do you recommend any good books about python unit testing ? on how to write good tests and avoid antipatterns ?

maiden pawn
#

I used to get general book about Unit Testing, it looked more promising, with examples in C#

crude geyser
#

well, I don't know about TDD, it always seemed to me as an antipattern, I'm afraid that people would invest less effort on writing tests, consider tests as a chore rather than investing time on writing them

#

"just make sure tests are passing so I can check to boxes and consider development is done"

#

but I wouldn't mind a general book even if it's written in a different language

tiny widget
#

I always considered tests as a chore. That’s not something that I enjoy writing, even if they are necessary to the project life cycle.

maiden pawn
#

also tells about all those traps

#

to ensure you don't fall into them

#

oh it was translated

#

I liked how... I just opened the book in the middle
and it told me, run your tests on actual copy of your database data if you can
if will ensure 'something something' that will make your tests better

#

well, I didn't read it yet, but looks good from afar

crude geyser
#

Thank you @maiden pawn I'll look into it, looks interesting indeed

sly swallow
#

Okay so I have this middleware

#

and I need to write a test for it

#
  def process_response(self, request, response, spider):
        for ce in self.cost_estimators:
            if ce.is_cost_type(request, response):
                cost_type = ce.cost_type_id()
                self.stats.inc_value(f'costestimator/{cost_type}/response_count', spider=spider)
                res_len = len(response_httprepr(response))
                self.stats.inc_value(f'costestimator/{cost_type}/decompressed_response_bytes', res_len, spider=spider)
        return response
#

and

#
    def process_request(self, request: scrapy.Request, spider: scrapy.Spider):
        for ce in self.cost_estimators:
            if ce.is_cost_type(request):
                cost_type = ce.cost_type_id()
                self.stats.inc_value(f'costestimator/{cost_type}/request_count', spider=spider)
                req_len = len(request_httprepr(request))
                self.stats.inc_value(f'costestimator/{cost_type}/request_bytes', req_len, spider=spider)
        return None
#

How do I think of tests

#

I need to mock the request and response?

steady frigate
#

In Python, what are the different ways to test a program? I've implemented a Flask-based webapp which uses a couple of Python scripts to execute some process.. there's a lot of functions and procedures written.. can anyone give me some advise on how I might test the application with use of white box testing methods as well as unit tests & integration tests?

maiden pawn
steady frigate
maiden pawn
#

pylint for that is good

steady frigate
bitter wadiBOT
#

Hey @maiden pawn!

It looks like you tried to attach file type(s) that we do not allow (.zip). We currently allow the following file types: .gif, .jpg, .jpeg, .mov, .mp4, .mpg, .png, .mp3, .wav, .ogg, .webm, .webp, .flac, .m4a.

Feel free to ask in #community-meta if you think this is a mistake.

maiden pawn
#

gathering repository

steady frigate
maiden pawn
#

as you can see all tests grouped in folder test

#

with further grouping in sub folders

#

and within one folder, you can have multiple files of tests

#

in scripts, I left example how to run all tests from particular folder

#

(remove particular folder name, and just run on folder test, to run all of them at once)

#

and how to run one specific method is mentioned in scripts

#

for further comfortability...
I could recommend thinking about using some
library to run console commands with a lot of flexibility in flags
like
import click
or something like that

#

it could cover remaining needs

#

deleted some unecessary trash and updated

steady frigate
maiden pawn
#

I gave it as example how to group pytest tests and launch them

#

but what would you write as content, is your business

steady frigate
maiden pawn
#

you would probably also find as useful

#

that for all tests

#

single app initialization in this file)

steady frigate
#

That's the thing I don't really know what part of my app I should be testing..

maiden pawn
#

usually code gets organized in subfolders/sections

steady frigate
maiden pawn
#

I test those sections of applications, each in different file

maiden pawn
#

having ability to run particular tests as fast and often as possible

#

as for grouping in folder for me... it made comfortable usage, to run different groups of tests for different conditions in github actions

#

plus...

#

grouping in folders and files... your tests have... organization like it is another application

#

the point is... that continuing support of tests, their refactorization and e.t.c. takes time too

#

so you need to write them organized and clean like your main code

#

basically I already wrote 3 reasons why it should be done so

steady frigate
sly swallow
#

I think you mock the other dependencies?

steady frigate
maiden pawn
# steady frigate If we test sections of application, I don't get how would it work logically? Coz...

You are supposed to do minimal initialization, suiting all tests

And after that to write tests, which... check that every component, funcion, class, module work correctly.
Separately from each other.

Anyway.. There is a bit if refactorizarion involved. If you don't know how to test different parts if application with comfort .. it usually means... the program needs refactorizatiin/cleaning/restructurizarion in order to become good enough for testing

Tests must be as isolated from each other as possible. You aren't supposed to write tests which depend on previous one to happen.
Each test should initialize for itself, all necessary data during its start

maiden pawn
white brook
#

Hello their! It say it doesn't find my file but why?

#

My tree structure on right

low pagoda
white brook
low pagoda
#

Do you have any setup.py?

white brook
#

Okay but even with __init__.py it return the same.

low pagoda
# white brook Nop but what i need to put inside?

My typical setup.py contains

#!/usr/bin/env python3

from setuptools import find_packages, setup


setup(
    packages=find_packages(),
    version="0.0.0",
)

Other informations like package name or description I put in setup.cfg pithink

white brook
#

Ooooh, and where i put this file in my root directory or inside a specific file.

#

Okay i see, but it doesn't fix for my tests and i don't understand why it doesn't work because with other project i do before, it work.

low pagoda
white brook
low pagoda
#

In tests I am importing everything by using from mypackage import ...

sly swallow
#

I have a function that logs some stats. How do I write a test for that?

low pagoda
sly swallow
#
def on_start(self, cls):
        self.task = task.LoopingCall(self.log, cls)
        self.task.start(self.interval)
#
 def log(self, cls):
        total_cost = self.get_total_cost(cls)
        cost_per_minute = total_cost - self.cost_prev
        self.cost_prev = total_cost
        msg = f"Total Cost: {total_cost} at Cost Per Minute: {cost_per_minute}"
        logger.info(msg, extra={'cls': cls})
#

Something like this

low pagoda