Skip to content

Instantly share code, notes, and snippets.

View MGough's full-sized avatar

Merlin Gough MGough

View GitHub Profile

Keybase proof

I hereby claim:

  • I am mgough on github.
  • I am merlingough (https://keybase.io/merlingough) on keybase.
  • I have a public key ASDg384GHcuTZFSaPJSSvf0gNA-yPUHFlBiln0Aq-IGXvQo

To claim this, I am signing this object:

@MGough
MGough / Dockerfile
Created June 23, 2019 20:49
Example Base dockerfile
FROM python:3.7
RUN mkdir /app/
WORKDIR /app/
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY our_codebase /app/our_codebase
COPY some_script.py /app/
@MGough
MGough / test.Dockerfile
Last active June 23, 2019 21:07
Docker file to build a testing container
ARG BUILD_VERSION
FROM our-service:$BUILD_VERSION
COPY test /app/test
COPY dev-requirements.txt /app/
RUN pip install -r dev-requirements.txt
CMD python -m pytest --junitxml /app/results.xml --cov=/app/our_codebase --cov-report xml test/
@MGough
MGough / Jenkinsfile
Created June 23, 2019 21:03
Python testing jenkinsfile
// This is just a snippet, not a complete file
try {
stage('Unit Tests') {
// Build our base image
app = docker.build("our-service:PR-${BUILD_ID}")
// Build our test image passing our base image version and our test dockerfile
docker.build("our-service-test:PR-$BUILD_ID", "--build-arg BUILD_VERSION=PR-$BUILD_ID . -f test.Dockerfile")
@MGough
MGough / example_object_to_test.py
Created November 17, 2019 22:01
An example of an object to test.
from src.imported_class import SomeClassOutOfOurControl
class SomeObjectToTest:
some_property = 5
some_other_property = 10
def __init__(self):
self.calculator = SomeClassOutOfOurControl()
@MGough
MGough / example_object_someone_else_implemented.py
Created November 17, 2019 22:04
An abstract class to be used for a unittest.mock example.
from abc import abstractmethod
class SomeClassOutOfOurControl:
@abstractmethod
def add(self, a, b):
raise NotImplementedError("We don't care about this")
@MGough
MGough / test_example_object.py
Last active November 17, 2019 22:15
An example of *fragile* unit tests due to bad mocks.
from unittest.mock import patch, Mock
from pytest import fixture
from src.example import SomeObjectToTest
class TestSomeObjectToTest:
@fixture
def expected_result(self):
return 400
@MGough
MGough / example_object_someone_else_implemented_v2.py
Created November 17, 2019 22:17
A modified class out of our control
from abc import abstractmethod
class SomeClassOutOfOurControl:
@abstractmethod
def add(self, items):
raise NotImplementedError("We don't care about this")
@MGough
MGough / more_reliable_mocks.py
Created November 17, 2019 22:45
Mocking using autospeccing.
from unittest.mock import patch, Mock, create_autospec
from pytest import fixture
from service.example import SomeObjectToTest
from service.imported_class import SomeClassOutOfOurControl
class TestSomeObjectToTest:
@fixture
def expected_result(self):
a:
00863395735779106D01A3B: 30
00863531904040789C8A4C6: 31
00863604179437128DD9A2B: 32
00873485276243227609735: 33
0087352270643094413B0D7: 34
008736034269259128AC6C7: 35
00883394559972599E77AEC: 36
008B3600524843455E61C85: 37
008C3378065645906E77882: 38