AttributeError: module 'anyio._backends._asyncio' has no attribute 'run' - python

I am trying to test below code for Fastapi
from fastapi.testclient import TestClient
import unittest
client=TestClient(app)
class TestTelemetryAdapter(unittest.TestCase):
def test_ready(self):
a=client.get('/readiness')
self.assertEqual(a.status_code, status.HTTP_200_OK)
but I am getting error: AttributeError: module 'anyio._backends._asyncio' has no attribute 'run' for line: a=client.get
my python ver: 3.9.10
don't want to use async with func def

It is resolved after deleting the .pyc files of "asyncio" and "anyio".

Related

Get response in Django testing?

I have a problem with getting response in django testing
this is the problem appear in terminal
self.assertEquals(self.response.status_code, 200)
AttributeError: 'StoreCreateTest' object has no attribute 'response'
This is the command
py manage.py test
This is my def
def test_store_create_view_status_code(self):
self.assertEquals(self.response.status_code, 200)
and this is my import
from django.test import TestCase
from configuration.models import Stores
please help me getting this.

Unit Testing AWS python lambda global variable patch doesnt work

I am trying to unit test Python based aws lambda code. I tried to use patch to mock environment variables but it throws error.How to handle global variables during unit testing. Any help is appreciated
Tried #mock #patch but nothing seems to be working. When executing the testcase the values seems to be empty
[registration.py]
import os
#Global Variables
DEBUG = os.environ.get("EnableLog")
rest_endpoint = os.environ.get('restApiEndpoint')
db_fn_endpoint = rest_endpoint+":3000/rpc/"
def lambda_handler(event,context):
return "success" #to see if this works
if __name__ == "__main__":
lambda_handler('', '')
Unit Test [test_registration.py]
import json
import pytest
import sys, os
from mock import patch
from businesslogic import registration
#mock.patch.dict(os.environ,{'EnableLog':'True'})
#mock.patch.dict(os.environ,{'restApiEndpoint':'http://localhost'})
#patch('registration.restApiEndpoint', 'http://localhost')
def test_lambda_handler():
assert lambda_handler() =="success"
When I run pytest test_registration.py
I get below exception
businesslogic\registration.py:6: in <module>
db_fn_endpoint = rest_endpoint+":3000/rpc/"
E TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
You can pass it as restApiEndpoint=http://localhost python -m pytest
OR
you if you are using sh file to run export before running test
export restApiEndpoint=http://localhost

module "twitter" has no attribute "Twitter"

I have the following file, twitter.py, which defines a class called Twitter:
class Twitter:
data = {}
def __init__(self):
pass
def tweet(self):
print("I'm tweeting")
And I have another file, main.py, in the same directory as twitter.py which imports twitter and attempts to instantiate the class:
import twitter
twitterObj = twitter.Twitter()
Unfortunately, Python throws the error message: AttributeError: module 'twitter' has no attribute 'Twitter'
What am I doing wrong?
Can't reproduce this error on my machine. I'm assuming you have a module with the name "twitter" installed to your python path that is overriding the project file. Try renaming twitter.py.

AttributeError: 'module' object has no attribute 'ZKLib'

I'm trying to connect to biometric device. I have installed 'Zklib' using (Pip). My code as follows
`import sys
import zklib
import time
from zklib import zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret`
When I execute this, I get an error
AttributeError: 'module' object has no attribute 'ZKLib'
Help me to run this code successfully.
Try the following instead:
import sys
import time
from zklib import zklib, zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret
The ZKlib class is in zklib.zklib not zklib. It appears that there is a typo in the Getting Started section of their GitHub page (or they expect you to be in the zklib directory when running your code?).

Error when trying to use `assert not mock.method.called`

I'm trying to assert that a method is not called using Python Mock. Unfortunately, I can't seem to get past this error:
AttributeError: MockCallable instance has no attribute 'called'
I'm running Python 2.7.1 and Python Mock 0.1.0 for my tests. Google says: No results found for "AttributeError: MockCallable instance has no attribute 'called'". How can I resolve this error?
Here's the test:
import unittest2
import main
from mock import Mock
class TestCli(unittest2.TestCase):
def setUp(self):
self.mockView = Mock()
self.mockFriendManager = Mock()
self.mockedCli = main.CLI(self.mockView, self.mockFriendManager)
[...]
def testCliDoesntGetFriendPropertiesWhenNotSelected(self):
view = Mock( { "requestResponse":2 } )
friendManager = Mock()
cli = main.CLI(view, friendManager)
cli.outputMenu()
assert not friendManager.getFriendProperties.called, 'hello'
You must update you mock library by pip. Attribute called was introduced in 0.4.0 as you can see in http://www.voidspace.org.uk/python/mock/changelog.html#version-0-4-0
Anyway by update it you will get a lot of more useful facilities and tools.

Categories

Resources