[Python] Test sample using unittest2, mock

Directory structure

$ tree -I "*pyc"
.
├── src
│   └── hoge.py
└── test
    └── hoge_test.py

Product code

hoge.py


1 class Hoge:
2   def moge(self):
3     return True

Test code

hoge_test.py


 1 try:
 2     import unittest2 as unittest
 3 except (ImportError):
 4     import unittest
 5 from mock import *
 6
 7 import sys, os
 8 dir_path = os.path.dirname(os.path.abspath(__file__))
 9 sys.path.append(dir_path + '/../src')
10 from hoge import Hoge
11
12
13 class TestHoge(unittest.TestCase):
14
15   def setUp(self):
16     self.sut = Hoge()
17
18   def test_hoge_mock(self):
19     self.sut.moge = MagicMock(return_value=False)
20     self.assertFalse(self.sut.moge())
21
22   def test_hoge_raw(self):
23     self.assertTrue(self.sut.moge())
24
25 if __name__ == '__main__':
26   unittest.main()

test results

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Recommended Posts

[Python] Test sample using unittest2, mock
AtCoder: Python: Daddy the sample test.
Common mock by moto in Python Unittest
python setup.py test the code using multiprocess
Unittest in python
Python closure sample
Start using Python
Python Integrity Test
Scraping using Python
Aggregate test results using the QualityForward Python library
Primality test with Python
Operate Redmine using Python Redmine
Python basics 8 numpy test
Python test package memo
python unittest assertXXX list
Ajax + Python + PostgreSQL sample
Data cleaning using Python
Primality test with python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
python tag integration test
Name identification using python
Notes using Python subprocesses
python unit test template
Try using Tweepy [Python2.7]
Mock urllib2 and unittest
Python --Simple multi-thread sample
How to test that Exception is raised in python unittest
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
Sample data created with python
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Test CloudStack API Using Simulator
GitHub Actions Python cache sample
Python standard unittest usage notes
Try using Kubernetes Client -Python-
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Post to Twitter using Python
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Python template for Codeforces-manual test-
Web scraping using Selenium (Python)
Python higher-order function (decorator) sample
[Python] I tried using OpenPose
Python debug and test module
[Python] JSON validation using Voluptuous
A memorandum about Python mock