我正在为自定义错误类编写测试并希望涵盖所有行,但我不确定如何测试超级调用,我还想在创建时返回提供给对象的条形码。
import requests
url = 'https://youtube.googleapis.com/youtube/v3/videos?part=snippet&part=status&autoLevels=true¬ifySubscribers=true'
access_token = "ya29.a0ARrdaM9hqYdrZTLyGCkg32zznNdgguSFyP6QPAZRJiLTVKvMlkT7pQth8bTzxLN4qdLy_fgx7fyItoElqX5f8Ht1jfvU6MiV8l9fauuYY-qOAwbW6yxMZJk1FbQikK4_ElrhvSpgs4JSZpHsytfu1b36EQDA"
token ="Bearer "+access_token
payload = {
'snippet': {
'categoryId': 24,
'title': 'Upload Testing',
'description': 'Hello World Description',
'tags': ['Travel', 'video test', 'Travel Tips']
},
'status': {
'privacyStatus': 'private',
'selfDeclaredMadeForKids': False,
},
'notifySubscribers': False
}
# Set up headers and payload for first authentication request
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization":token,
"mimetype":"video/*"
}
files = {'file': open('Video.mp4', 'rb')}
response = requests.request("POST", url, json=payload, files=files, headers=headers)
response = response.json()
print(response)
#videoId = response["videoId"]
#print(videoId)
答案 0 :(得分:0)
编写测试应该非常简单。覆盖意味着“在运行我的测试时是否执行了这一行”。所以调用 BarcodeDecoderError
的构造函数应该可以解决问题。当然,您可能还想测试 super 调用是否具有预期效果:
it('instantiates BarcodeDecoderError with all properties', () => {
const error = new BarcodeDecoderError('message', 'barcode', ErrorType.Something);
expect(error.message),toBe('message'); // Test if super call worked
expect(error.barcode).toBe('barcode');
expect(error.errorType).toBe(ErrorType.Something);
})
(可能会根据您的测试框架进行调整)
全面覆盖通常仅在涉及分支(if
、switch/case
、条件loops
)或您正在构建库时才会成为问题公开代码中未使用的函数。
在您的具体示例中,在实际示例中测试 BarcodeDecoderError
并且将覆盖范围作为副产品可能就足够了:
it('throws BarcodeDecoderError with all properties', () => {
// Decode something where you would expect an error
const decoder = new BarcodeDecoder();
expect(decoder.decode()).toThrow(expect.objectContaining({
message: 'message',
barcode: 'barcode',
errorType: ErrorType.Something
}))
})