0

I am trying to use LocustIO for load testing. To make a POST call, I passed the json params to it and it returned the value. I created an expected response value because that is what it is expected to return. Now what I have been trying to do is to compare the expected value and the response value, I kept on getting this error:

s...
[2018-04-10 10:32:23,351] DESKTOP-MJ573AA/INFO/stdout: Response status code:
[2018-04-10 10:32:23,351] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: 201
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: Response status code:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: b'[\n  {\n    "userId": 1,\n    "id": 1,\n    "title": "delectus aut autem",\n    "completed": false\n  }\n]'
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,354] DESKTOP-MJ573AA/ERROR/stderr: Traceback (most recent call last):
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 271, in run
    self.execute_next_task()
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 297, in execute_next_task
    self.execute_task(task["callable"], *task["args"], **task["kwargs"])
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 309, in execute_task
    task(self, *args, **kwargs)
  File "C:\Programs\Others\Locust\locustfile.py", line 37, in send
    a = json.loads(responseValue)
AttributeError: 'str' object has no attribute 'loads'

I have tried all the suggested options on SO and none seems to work. Sorry, this is just my 3rd day of learning python. An example - Python 'str' object has no attribute 'read' Code:

from locust import HttpLocust, TaskSet, task
from slumber import API 
import json, requests

json = """
[{
  userId: 1
}]
"""

jsonsecond = """
[{
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  }]
"""

responseValue = """
[{
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  }]
"""

json_response = dict()
class MyTaskSet(TaskSet):
  @task(1)
  def send(self):
    response = self.client.post("/todos", jsonsecond, headers= { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Token eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiYzQ3M2U0OWViODkyNGM4YTdjMDkxNmZhZmFjODgyIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1MTE4ODk5NzIsImV4cCI6MTUxNDQ4MTk3MiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkxIl0sImNsaWVudF9pZCI6ImNsaWVudCIsInNjb3BlIjpbImFwaTEiXX0.Qku54rtPbPyyEubJekXwKlbmKXEI3hABPVYNZzpAv69Dii8Ud42ucDyZ90gjyZ3yF7v5XiK9s_ZuKmiL2IxJF4zDc4dal78T7B9gnwmSatyWPRBOKjJw8N_qcuaJlnOsvVVAZ2JuMqZion7GX8W8ykbG3xjB-EAPpyYCoP7YEI8qqMQ6PLKpLi_RwYNt3ATF6nzOQIIV4McfroNPuRKfHAszgLqPMPw2bWqcgFzSEeQTTzU2lQieQNr3vOsUVmW6DX5QpdpwYSW4fNeEByVgcCrvPx-ZnPm85HR4nthEAV1Y1yoxJUzgl1wOu9k2JadRdjnF_8UVWriO96zKb2PDDg' } )
    print("Response status code:", response.status_code)
    print("Response status code:", response.content)

    a = json.loads(responseValue)
    b = json.loads(response.content)
    if sorted(a.items()) == sorted(b.items()):
      print('good. application success')

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000
    # host = "http://www.somaku.com"
    host = "https://jsonplaceholder.typicode.com"
    stop_timeout = 200
jpp
  • 159,742
  • 34
  • 281
  • 339
ken4ward
  • 2,246
  • 5
  • 49
  • 89
  • 1
    Possible duplicate of [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – SiHa Apr 10 '18 at 09:48

1 Answers1

3

Do not name variables after classes.

In this instance, replace:

json = """
[{
  userId: 1
}]
"""

with the following:

json_str = """
[{
  userId: 1
}]
"""

Your code should then run as expected.

jpp
  • 159,742
  • 34
  • 281
  • 339