Skip to content

Instantly share code, notes, and snippets.

Avatar

Liam LiamJolly

View GitHub Profile
View multiple_calls.py
def function_one():
print "One"
def function_two():
print "Two"
def function_three():
print "Three"
@LiamJolly
LiamJolly / MyException.java
Created May 6, 2017
An example Java exception to demonstrate what is important when it comes to testing.
View MyException.java
package com.liamdjolly;
public class MyException extends Exception {
public MyException() {
}
public MyException(String message) {
super(message);
}
@LiamJolly
LiamJolly / exception_example_test.py
Last active Apr 2, 2017
The unit test for our simple exception_example, uses side_effect to trigger the exception.
View exception_example_test.py
import exception_example
import unittest
import mock
class ExceptionExampleTest(unittest.TestCase):
def test_does_user_exist(self):
self.assertTrue(exception_example.does_user_exist("user1"))
def test_does_user_exist_no_user(self):
@LiamJolly
LiamJolly / exception_example.py
Last active Apr 2, 2017
A really simple example to help demonstrate mocks that throw exceptions.
View exception_example.py
def find_user(username):
if username == "user1":
return dict(name="user1", id_no="12345")
else:
raise Exception
def does_user_exist(username):
try:
find_user(username)
@LiamJolly
LiamJolly / file_utils_test.py
Last active Mar 15, 2017
Unit tests for file_utils.py, demontrating mock_open.
View file_utils_test.py
import unittest
import mock
import file_utils
class FileUtilsTest(unittest.TestCase):
def test_read_file(self):
with mock.patch("__builtin__.open", mock.mock_open(read_data="MOCKED"), create=True) as mock_file:
result = file_utils.read_file("path")
@LiamJolly
LiamJolly / users_test4.py
Last active Mar 15, 2017
Testing using mock deocorators.
View users_test4.py
import unittest
import mock
import users
import file_utils
class UsersTest(unittest.TestCase):
_file_path = "INSERT_PATH_HERE"
@mock.patch("file_utils.read_file", return_value="user1")
@LiamJolly
LiamJolly / users_test3.py
Last active Mar 15, 2017
Testing using mocks and with form.
View users_test3.py
import unittest
import mock
import users
import file_utils
class UsersTest(unittest.TestCase):
_file_path = "INSERT_PATH_HERE"
def test_get_users(self):
@LiamJolly
LiamJolly / users_test2.py
Last active Mar 15, 2017
Demonstrates the dangers and scope of this kind of mocking.
View users_test2.py
import unittest
import mock
import users
import file_utils
class UsersTest(unittest.TestCase):
_file_path = "INSERT_PATH_HERE"
def test_get_users(self):
View file_utils.py
def write_file(path, content):
file_to_write = open(path, "w")
file_to_write.write(content)
file_to_write.close()
def read_file(path):
with open(path, 'r') as file_to_read:
data = file_to_read.read()
return data
@LiamJolly
LiamJolly / users_test.py
Last active Mar 15, 2017
Simple example of mocking two functions for the purpose of testing.
View users_test.py
import unittest
import mock
import users
import file_utils
class UsersTest(unittest.TestCase):
_file_path = "SOME FILE PATH"
def test_get_users(self):
You can’t perform that action at this time.