https://github.com/optimizely/unit-testing-training
https://github.com/optimizely/unit-testing-training
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/optimizely/unit-testing-training
- Owner: optimizely
- Archived: true
- Created: 2016-03-05T00:16:33.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-05T00:50:54.000Z (about 10 years ago)
- Last Synced: 2025-03-01T12:17:15.601Z (about 1 year ago)
- Language: Python
- Size: 1.95 KB
- Stars: 0
- Watchers: 130
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# unit-testing-training
This repo provides a Python module (`functions.py`) and a corresponding module (`functions_test.py`) containing unit
tests for `functions.py`.
## Setup
After cloning this repo:
```
git clone git@github.com:optimizely/unit-testing-training.git
cd unit-testing-training
```
... run the shell script to install dependencies and create a virtualenv (this will ask for your system password):
```
./setup.sh
```
Then enter your new virtualenv:
```
source .virtualenv/bin/activate
```
And finally run all the Python tests in this repo:
```
python -m unittest discover
```
## Where To Start
Try breaking one of the tests and rerunning the tests. In `functions.py`, change:
```
def add_three_to_value(value):
return value + 3
```
to
```
def add_three_to_value(value):
return value + 4
```
Then once again run:
```
python -m unittest discover
```
And you should see something like:
```
======================================================================
FAIL: test_add_three_to_value__with_seven (test_my_functions.MyFunctionsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/username/repos/unit-testing-training/test_my_functions.py", line 15, in test_add_three_to_value__with_seven
self.assertEqual(10, my_functions.add_three_to_value(7))
AssertionError: 10 != 11
======================================================================
FAIL: test_add_three_to_value__with_ten (test_my_functions.MyFunctionsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/username/repos/unit-testing-training/test_my_functions.py", line 18, in test_add_three_to_value__with_ten
self.assertEqual(13, my_functions.add_three_to_value(10))
AssertionError: 13 != 14
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
```
Hopefully that helps makes the setup a bit more concrete. Fiddle freely with the tests!
## More Reading/Viewing
* Rambling presentation on unit testing using this repo: https://drive.google.com/a/optimizely.com/file/d/0B3DMe9ZATniSWnZqdHdFQ3J1T0E/view?usp=sharing
* https://docs.python.org/2/library/unittest.html
* https://docs.python.org/3/library/unittest.mock.html
* https://www.toptal.com/python/an-introduction-to-mocking-in-python