Created
May 4, 2020 18:19
-
-
Save akrisanov/f7e634e6ccdc176173e9b7aca109a8e6 to your computer and use it in GitHub Desktop.
Pytest Fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
# Environment Variables | |
@pytest.fixture(autouse=True) | |
def env_setup(monkeypatch): | |
monkeypatch.setenv('MY_SETTING', 'some-value') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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