应用程序所需的测试登录

2024-06-16 11:03:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个flaskweb应用程序编写测试,每个测试都需要用户登录 有没有办法定义测试函数并在其他测试中使用它? 这是我当前的测试用例,我只想使用post to API URL实现登录函数

class TestLogin(unittest.TestCase):
   def setUp(self):
       self.headers = {'Content-type':'application/json','Accept':'tex/plain')
   def test_login(self):
      s=request.Session()
      url = 'http://localhost/api/v1/user/login/'
      data = {'username' = 'admin','password' :'123123'}
      r=s.post(url,data=json.dumps(data),headers=self.headers)
      self.assertEqual(r.status_code,200)

Tags: 用户selfjson应用程序urldata定义def
1条回答
网友
1楼 · 发布于 2024-06-16 11:03:14

您可以创建一个单独的登录函数。例如

  def login():
      s = request.Session()
      url = 'http://localhost/api/v1/user/login/'
      data = {'username' = 'admin','password' :'123123'}
      r=s.post(url,data=json.dumps(data),headers=self.headers)
      return (r.status_code == 200)

然后从测试中调用它:

 def test_something(self):
      if(login()):
         #Test stuff

相关问题 更多 >