Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

Python Unit test for method contains request

I have a method, which contains external rest-api calls.

ex:

def get_dataset():
    url=requests.get("http://api:5001/get_trainingdata")
    filename=url.text[0]
    return filename

When I do @patch for this function, I can able to do unittest. But, coverage in not covering whole function. How can write unittest case for this method with full coverage?

My testcase

@mock.patch('api.get_dataset')     
def test_using_decorator1(self, mocked_get_dataset):                  
file = [{"file":"ddddd"}]         
mocked_get_dataset.return_value = Mock()         mocked_get_dataset.return_value.json.return_value = file        
 filename = file[0]         
self.assertEqual(filename, file[0])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...