简体   繁体   中英

Minimal audio (wav or mp3) file in bytes for unit testing

I want to find a minimal audio file (like the testfile_gif below) for unit testing.

I don't want to load it from the hard drive (like here ).

I want the second test to perform like the first one.

import magic
from django.core.files.uploadedfile import SimpleUploadedFile


class TestFiles(TestCase):
    def test_working(self):
        # Test an Image File #
        testfile_gif = (
            b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00\x00\x00\x21\xf9\x04'
            b'\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02'
            b'\x02\x4c\x01\x00\x3b')
        gif_file = SimpleUploadedFile(name='image.gif', content=testfile_gif,
                                      content_type='image/gif')
        mime = magic.from_buffer(gif_file.read(1024), mime=True)
        self.assertEqual('image/gif', mime)

    def test_not_working(self):
        # Test an Audio File #
        testfile_audio = b'What should be written in here?'
        audio_file = SimpleUploadedFile(name='music.mp3',
                                        content=testfile_audio,
                                        content_type='audio/mpeg')
        mime = magic.from_buffer(audio_file.read(1024), mime=True)
        self.assertEqual('audio/mpeg', mime)

Preferably, I don't want to use any packages (like import mock ).

UPDATE

Here is an mp3 file with audio/mpeg mime:

b'MM\x00*\x00\x00\x00\x08\x00\x03\x01\x00\x00\x03\x00\x00\x00\x01\x00\x01'
b'\x00\x00\x01\x01\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01\x11\x00\x03'
b'\x00\x00\x00\x01\x00\x00\x00\x00'

Here's a very simple .wav file. You can make these with python quite easily using the wave module.

b'RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x00\x04\x00\x00\x00\x04\x00\x00\x01\x00\x08\x00data\x00\x00\x00\x00'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM