Skip to content

Instantly share code, notes, and snippets.

@akrisanov
Created May 4, 2020 18:19
Show Gist options
  • Save akrisanov/f7e634e6ccdc176173e9b7aca109a8e6 to your computer and use it in GitHub Desktop.
Save akrisanov/f7e634e6ccdc176173e9b7aca109a8e6 to your computer and use it in GitHub Desktop.
Pytest Fixtures
import pytest
# Environment Variables
@pytest.fixture(autouse=True)
def env_setup(monkeypatch):
monkeypatch.setenv('MY_SETTING', 'some-value')
import pytest
import moto
import boto3
TEST_BUCKET_NAME = 'test-bucket'
# AWS S3
@pytest.fixture
def s3_bucket():
with moto.mock_s3():
boto3.client('s3').create_bucket(Bucket=TEST_BUCKET_NAME)
yield boto3.resource('s3').Bucket(TEST_BUCKET_NAME)
import pytest
import PyPDF2
@pytest.fixture
def pdf_file(tmpdir_factory):
file_name = 'pdftest.pdf'
# Fill with blank pages
writer = PyPDF2.PdfFileWriter()
for _ in range(num_pages):
writer.addBlankPage(width=100, height=100)
writer.addBookmark(title='Intro', pagenum=0)
fn = tmpdir_factory.mktemp('tmp', numbered=True).join(file_name)
with open(fn, 'wb') as pdf:
writer.write(pdf)
return fn
class MyTest:
def test_upload(self, s3_bucket):
my_upload_func()
# Assert that the file is in the bucket
files_in_bucket = s3_bucket.objects.filter(Prefix=f'{SOME_ID}/')
assert len(list(files_in_bucket)) == 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment