我该如何进行http.server do_GET模拟单元测试?

时间:2018-06-28 09:10:07

标签: python-3.x unit-testing magicmock

我是单元测试的初学者。我想知道如何对以下函数进行模拟单元测试。

import ...

class A(BaseHTTPRequestHandler):

   def do_GET(self):
      client_ip = self.client_address[0]
      if client_id == '10.10.10.10':
         self._set_headers_200()
         return
      else:
        self._set_headers_400()
        return

测试类:

import unittest

def test_A_get():

   I want to test both 200 and 400 response

有人可以帮助我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我正在从事类似的任务。到目前为止,这是我提出的部分解决方案

@IBAction func UserTap_Handler(_ sender: Any) {

        self.navigationController?.isNavigationBarHidden = false
        let con = CNContact()
        let vc = CNContactViewController(forNewContact: con)
        vc.delegate = self
        _ = self.navigationController?.pushViewController(vc, animated: true)
    }

    //MARK:- contacts delegates
    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        print("dismiss contact")
        self.navigationController?.popViewController(animated: true)
    }
    func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
        return true
    }
相关问题