2
我有查看功能,它使用nmap来扫描网络中的设备。使用模拟测试django应用程序来覆盖功能
views.py
import nmap
def home(request):
y=nmap.PortScanner()
data = y.scan(hosts="192.168.1.*", arguments="-sP")
context[status]=data['status']['addresses']['ipv4']
return render_template('home.html',context)
现在我要测试的这款为no devices
,1 device connected
和2 or more device connected
。我需要覆盖tests.py中的数据。
我在想,它可以使用模拟功能来完成。我可以在tests.py中覆盖它,但是当模拟响应时它不会在视图函数中得到覆盖。
我该如何测试这个nmap函数?
your_module意味着我的views.py,对不对? –
可能是的。它是您的主页视图被定义的模块。实际上,它看起来像:'from yourapp import views' – ohannes
但是这里的模拟函数是如何工作的。没有从模拟进口。 MockPortScanner –