我已经搜索了很多关于这个但没有找到任何东西......
我有一个自定义异常类,它调用了一个QDialog来显示警告消息。现在我正在为我的应用程序编写测试,在测试中,我发送的数据将使应用程序引发该异常并因此对话会出现。
但是当我使用pt.test
运行测试时,对话框出现了,当我关闭它时,它会打印在我的控制台上。
E Failed: DID NOT RAISE
apptest.py:31: Failed
----------------------------- Captured stderr call -----------------------------
Traceback (most recent call last):
File "/home/kapil/mp3wav/mp3wav/application.py", line 62, in <lambda>
self.connect(self.conversionButton, SIGNAL("clicked()"), lambda: self.startConversion())
File "/home/kapil/mp3wav/mp3wav/application.py", line 79, in startConversion
self.__convert()
File "/home/kapil/mp3wav/mp3wav/application.py", line 86, in __convert
self.checkFile(__OUTFILE)
File "/home/kapil/mp3wav/mp3wav/application.py", line 105, in checkFile
raise FileTypeException(fileType, self)
mp3wav.exceptions.fileexception.FileTypeException
这是我的测试文件..
import pytest
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from mp3wav.application import Mp3WavApp
from mp3wav.exceptions.fileexception import FileTypeException
from mp3wav.exceptions.libraryexception import LibraryException
from mp3wav.exceptions.filenotexistexception import FileNotExistException
from mp3wav.exceptions.samefileexception import SameFileException
from mp3wav.exceptions.overwriteexception import OverWriteException
def windowTest(qtbot):
testapp = Mp3WavApp()
testapp.show()
qtbot.addWidget(testapp)
assert testapp.isVisible()
assert testapp.close()
@pytest.mark.qt_no_exception_capture
def fileTypeTest(qtbot, tmpdir):
testapp = Mp3WavApp()
qtbot.addWidget(testapp)
infile = tmpdir.mkdir("files").join("demo.mp3")
infile.write("something")
testapp.inputFileLine.setText(str(tmpdir.join("files", "demo.mp3")))
testapp.outputFileLine.setText(str(tmpdir.join('files')))
testapp.outputFileLineName.setText('demo.wave')
with pytest.raises(FileTypeException) :
qtbot.mouseClick(testapp.conversionButton, Qt.LeftButton)
这是自定义异常
import os
import sys
from mp3wav.exceptions.errordialog import ErrorDialog
class FileTypeException(Exception):
def __init__(self, sid, parent=None):
super(FileTypeException, self).__init__()
filetype = "mp3"
if sid==2:
filetype = "wav"
errorMessage = "The Selected file is not "+filetype+\
"\nSelect the appropriate file"
if parent:
dialog = ErrorDialog(parent, errorMessage)
dialog.show()
dialog.exec_()
else:
print(errorMessage)
我认为它失败了,因为对话框在测试运行期间出现,我需要手动关闭它。但我不知道测试失败的确切原因..
任何人都可以引导我走上正确的道路..非常感谢..