I'm having trouble coming up with a pytest function to test the function below:
Is there a way to mock any function/object such that I can fake a transcription result that's consistent every time I run the test and not send any data to the actual Azure Speech service (so it doesn't get billed)?
def transcribe_azure_speech_file(speech_file): speech_config = speechsdk.SpeechConfig(subscription=SUBSCRIPTION_KEY, region=REGION) audio_input = speechsdk.AudioConfig(filename=speech_file) auto_source_lang_config = speechsdk.AutoDetectSourceLanguageConfig(languages=["en-US"]) speech_recognizer = speechsdk.SpeechRecognizer( speech_config=speech_config, audio_config=audio_input, auto_detect_source_language_config=auto_source_lang_config ) transcripts = [] done = False def _stop_cb(evt): """callback that signals to stop continuous recognition upon receiving an event `evt`""" print('CLOSING on {}'.format(evt)) nonlocal done done = True def _recognized_event(evt): result = evt.result start_time = result.offset / 10e6 end_time = start_time + result.duration / 10e6 transcripts.append( { "transcript": result.text, "start_time": start_time, "end_time": end_time, } ) # connect callbacks to the events fired by the speech recognizer speech_recognizer.recognized.connect(_recognized_event) # stop continuous recognition on either session stopped or canceled events speech_recognizer.session_stopped.connect(_stop_cb) speech_recognizer.canceled.connect(_stop_cb) # start continuous speech recognition speech_recognizer.start_continuous_recognition() while not done: time.sleep(0.25) speech_recognizer.stop_continuous_recognition() return transcripts