Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the samples from pytest, but use unittest #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/unittest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

#### Setup

Does not need any additional libraries, as unittest is part of the
Does not need any additional libraries, as unittest and unittest.mock are part of the
standard Python library.


#### Running the tests

To execute the tests run `python -m unittest todo_project_name_test`
To execute the tests run `python -m unittest` from this directory `py -m unittest` on windows.

The methods starting with 'test_' are going to be discovered automatically.

Adding to vscode:

Navigate to beaker tab on the sidebar. Choose configure. Python tests may be supported out of the box on vs code. Choose unittest flavour. Choose test_* detection pattern. The test tab should now populate with all tests, and you can run and debug from the test tab.
12 changes: 12 additions & 0 deletions python/unittest/app/some_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import time


class SomeService:
def service_call(self, number):
# going into db looong complex processing
time.sleep(3)
return f"{number}"


def interact_with_service(service, number):
return service.service_call(number)
7 changes: 7 additions & 0 deletions python/unittest/app/thing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Thing:

def __init__(self, name):
self.name = name

def return_hello_name(self):
return "Hello " + self.name + "!"
69 changes: 69 additions & 0 deletions python/unittest/tests/test_double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from app.some_service import interact_with_service, SomeService
import unittest
import unittest.mock as mock

# TestDouble concep
# - https://martinfowler.com/bliki/TestDouble.html


#
# Example of using stubs
# 1. Hand written class - ServiceStub
# 2. using unittest.mock patch facilities
#


class ServiceStub:
@staticmethod
def service_call(number):
return "42"

class TestDouble(unittest.TestCase):

def test_stub(self):
result = interact_with_service(ServiceStub(), 0)
self.assertEqual(result, "42")

def test_stub_2(self):
expected = 'foo'

def method_stub(self, some_number):
return 'foo'

mock.patch(
'app.some_service.SomeService.service_call',
method_stub
)

actual = SomeService().service_call(expected)
self.assertEqual(expected, actual)

#
# Example of using mock:
#
@mock.patch('app.some_service.SomeService.service_call')
def test_mock_service_call_class(self, service_call) -> None:
rpc = SomeService()
rpc.service_call(-135)
SomeService.service_call.assert_called_with(-135)


@mock.patch('app.some_service.SomeService.service_call')
def test_mock_service_call_function(self, service_call) -> None:
rpc = SomeService()
interact_with_service(rpc, 100500)
SomeService.service_call.assert_called_once_with(100500)


def test_mock_service_call_object(self) -> None:
expected = 'foo'
rpc = SomeService()

mock.patch.object(
rpc,
'service_call',
return_value=expected,
autospec=True)

self.assertEqual(interact_with_service(rpc, expected), expected)

13 changes: 13 additions & 0 deletions python/unittest/tests/test_thing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from app.thing import Thing
import unittest

class TestThing(unittest.TestCase):

def test_correct_greeting(self):
thing = Thing("Bob")
self.assertEqual("Hello Bob!", thing.return_hello_name())


def test_fail(self):
thing = Thing("Albert")
self.assertEqual("Wrong!", thing.return_hello_name())
14 changes: 14 additions & 0 deletions python/unittest/tests/test_thing_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
from app.thing import Thing

class TestThingFixture(unittest.TestCase):
def setUp(self):
self.thing=Thing("Bob")

def test_correct_greeting(self):
self.assertEqual("Hello Bob!", self.thing.return_hello_name())


def test_fail(self):
self.assertEqual("Wrong!", self.thing.return_hello_name())

7 changes: 0 additions & 7 deletions python/unittest/todo_project_name_test.py

This file was deleted.