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.
Related
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".
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
Code below is unit test I'm writing for a flask app but results in the following error when I run the test:
File "/Users/ausername/projects/term_trader/tt2/tests/testRoutes.py", line 47, in test_deposit_route
response = self.app.post(BASE_URL,
AttributeError: 'TestRoutes' object has no attribute 'app'
Imports look okay to me as I had some other tests running out of a 'tests' folder with no issue. The routes themselves work just fine when I tested them in curl, I'm just trying to get in the habit of writing tests. Just can't seem to figure out what the issue is. This is flask version 1.0.3. Any advise is appreciated.
Code:
from unittest import TestCase
from model.user import User
from model.position import Position
from model.trade import Trade
from flask_app.app import app
from schema import build_user, build_positions, build_trades
import json
import os
BASE_URL = 'http://localhost:5000/api/'
class TestRoutes(TestCase):
def setup(self):
self.app.config['TESTING'] = True
self.app.config['DEBUG'] = False
self.app = app.test_client()
build_user()
build_positions()
build_trades()
bob = User(**{
"user_name": "bobdean",
"password": "password",
"real_name": "Bob Dean",
"balance": 0.0
})
bob.hash_password("password")
bob.api_key = "11111111111111111111"
bob.save()
def tearDown(self):
pass
def test_deposit_route(self):
bob = User.from_pk(1)
self.assertEqual(mike.user_name, "bobdean")
deposit = {"amount":1500.0}
response = self.app.post(BASE_URL,
data=json.dumps(deposit),
content_type='application/json')
self.assertEqual(response.status_code, 201, "Status Code should be 201")
self.assertEqual(bob.balance, 1500.0, "Bob's balance should equal 1500")
You need to rename setup to setUp according to the documentation
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?).
I am trying to write my first django webapp, and it work fine with a simple view but as soon as I include my model, it starts to give the following error
'module' object has no attribute 'getuid'
Request Method: POST
Request URL: http://localhost:8080/photos/
Django Version: 1.2.5
Exception Type: AttributeError
Exception Value:
'module' object has no attribute 'getuid'
Exception Location: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py in expanduser, line 321
Python Executable: /Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
I read that this might be because of circular import issue but I dont see anything in my model imports.
import logging
import sys
import os
import flickrapi
def get_photos_for_artist(artist=None):
if not artist:
logging.error('can not find photos for unknown artist')
return None
api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
flickr = flickrapi.FlickrAPI(api_key)
gen = flickr.walk(tags=artist, content_type=1, per_page=10)
return gen
def main():
pass
if __name__ == '__main__':
main()
What could be causing this?
Django Logs say :
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py in expanduser
return path
i = path.find('/', 1)
if i < 0:
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
import pwd
userhome = pwd.getpwuid(os.getuid()).pw_dir ...
else:
userhome = os.environ['HOME']
else:
import pwd
try:
pwent = pwd.getpwnam(path[1:i])
Try to check version of python and check python installation and PYTHONPATH variable. May be problim with enveroment, not code.