Let's say I have some code that accesses an audio device, such as the code below:
import pyaudio
def play_audio(audio):
"""Play audio array to the default audio device"""
if pyaudio.PyAudio().get_host_api_info_by_index(0).get('deviceCount') == 0 : # pragma: no cover
logging.error(NO_DEVICE_ERROR)
else : # ci: no cover
stream = pyaudio.PyAudio().open(
format=pyaudio.paFloat32, channels=2, rate=44100, output=True
)
stream.write(audio).tobytes()
I'd like to exclude from pytest code coverage lines 4-5 only during local builds (since I have an audio device), and 7-10 only during my CI build (since there is no audio device in the CI environment).
I've tried defining different keywords for skipping coverage in different situations ("pragma" for local, "ci" for continuous integration), but I have not found documentation on how to specify them from the command line.
How would I configure my .coveragerc to skip sections of the code based on the build parameters?
You can expand environment variables in the coverage settings file. You could put this into your .coveragerc:
Then annotation your code:
When you run tests locally, define an environment variable
COVERAGE_SKIP=local
. When you run tests on CI, defineCOVERAGE_SKIP=ci
.Of course, the wording of the comments, and the name of the environment variable are up to you.