我正在用python编写我的第一个单元测试,我已经用类和函数编写了通过测试,但是现在我想编写单元测试,但失败如下
import unittest
import requests
import time
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class ChromeSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
driver = self.driver
base_url = 'http://127.0.0.1:8080/'
driver.get(base_url)
def test_get_users(self):
print("Planning to get users")
token = test_generate_token()
headers = {'Content-Type': 'application/json',
'Token': token}
r = requests.get('http://127.0.0.1:8080/api/users', headers=headers)
print"Get request for api Users"
print(r.text)
def test_generate_token():
print("Generating Token")
usernameStr = 'user1a'
passwordStr = 'user'
response = requests.get('http://127.0.0.1:8080/api/auth/token', auth=(usernameStr, passwordStr))
print(response)
data = response.json()
print(data)
if response.status_code == 200 and data["status"] == "SUCCESS":
token = data["token"]
else:
token = None
print("The request was not successful.")
print(token)
return token
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
所以我在做的是从test_generate_token()
调用test_get_users(self)
来获取令牌,然后使用令牌来获取用户,但是失败并出现以下错误
Generating Token
<Response [200]>
{u'status': u'SUCCESS', u'token': u'OTEzMjQ0NjY1OTQ3NzM5OTYyMTczNTM0NzA4MjM0OTQ2NTQ5NDA='}
OTEzMjQ0NjY1OTQ3NzM5OTYyMTczNTM0NzA4MjM0OTQ2NTQ5NDA=
.
DevTools listening on ws://127.0.0.1:50610/devtools/browser/eb427541-898f-4c1d-938d-064462f50a68
Planning to get users
E
======================================================================
ERROR: test_get_users (__main__.ChromeSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".\test_demo_app.py", line 18, in test_get_users
token = test_generate_token()
NameError: global name 'test_generate_token' is not defined
我尝试遵循此Python: Passing variables between functions
令牌部分已移至设置
import unittest
import requests
import time
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class ChromeSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
driver = self.driver
base_url = 'http://127.0.0.1:8080/'
driver.get(base_url)
print("Generating Token")
usernameStr = 'user1a'
passwordStr = 'user'
response = requests.get('http://127.0.0.1:8080/api/auth/token', auth=(usernameStr, passwordStr))
print(response)
data = response.json()
print(data)
if response.status_code == 200 and data["status"] == "SUCCESS":
token = data["token"]
else:
token = None
print("The request was not successful.")
print(token)
def test_get_users(self):
print("Planning to get users")
headers = {'Content-Type': 'application/json',
'Token': token}
r = requests.get('http://127.0.0.1:8080/api/users', headers=headers)
print"Get request for api Users"
print(r.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
答案 0 :(得分:2)
由于test_generate_token
是成员函数,因此您需要将其称为成员函数。
token = self.test_generate_token()
。