Unit Testing Python-Chapter1
Unit Testing Python-Chapter1
U N I T T E S T I N G F O R D ATA S C I E N C E I N P Y T H O N
Dibya Chakravorty
Test Automation Engineer
How can we test an implementation?
def my_function(argument): my_function(argument_1)
...
return_value_1
my_function(argument_2)
return_value_2
my_function(argument_3)
return_value_3
Test
Test
PASS
Accepted
implementation
Test
FAIL PASS
Accepted
Bugfix
implementation
Test
FAIL PASS
Accepted
Bugfix
implementation
Feature request
or Refactoring
Test
FAIL PASS
Accepted
Bugfix
implementation
Feature request
or Refactoring
Test
FAIL PASS
Accepted
Bugfix
implementation
Feature request
Bug found
or Refactoring
Test
FAIL PASS
Accepted
Bugfix
implementation
Feature request
Bug found
or Refactoring
FAIL PASS
Accepted
Bugfix
implementation
Feature request
Bug found
or Refactoring
File: housing_data.txt
File: housing_data.txt
File: housing_data.txt
File: housing_data.txt
["2,081", "314,942"]
Argument Type Return value
["2,081", row_to_list("\t293,410\n")
"2,081\t314,942\n" Valid
"314,942"]
None
"\t293,410\n" Invalid None
None
FAIL PASS
Accepted
Bugfix
implementation
Feature request
Bug found
or Refactoring
Dibya Chakravorty
Test Automation Engineer
Testing on the console
row_to_list("2,081\t314,942\n")
["2,081", "314,942"]
row_to_list("\t293,410\n")
None
row_to_list("1,463238,765\n")
None
uni est
nosetests
doctest
Easiest to use.
Most popular.
import pytest
import row_to_list
import pytest
import row_to_list
def test_for_clean_row():
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_clean_row():
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_clean_row():
assert ...
assert True
assert False
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_clean_row():
assert row_to_list("2,081\t314,942\n") == \
["2,081", "314,942"]
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_missing_area():
assert row_to_list("\t293,410\n") is None
Do not do this.
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_missing_area():
assert row_to_list("\t293,410\n") is None
def test_for_missing_tab():
assert row_to_list("1,463238,765\n") is None
pytest test_row_to_list.py
Dibya Chakravorty
Test Automation Engineer
Unit tests for row_to_list()
Test module: test_row_to_list.py Argument Type Return value
["2,081",
import pytest "2,081\t314,942\n" Valid
"314,942"]
import row_to_list
def test_for_missing_area():
assert row_to_list("\t293,410\n") is None
def test_for_missing_tab():
assert row_to_list("1,463238,765\n") is None
def test_for_missing_area():
> assert row_to_list("\t293,410\n") is None
def test_for_missing_area():
assert row_to_list("\t293,410") is None # AssertionError from this line
def test_for_missing_area():
assert row_to_list("\t293,410") is none # NameError from this line
def test_for_missing_area():
> assert row_to_list("\t293,410\n") is None
E AssertionError: assert ['', '293,410'] is None
E + where ['', '293,410'] = row_to_list('\t293,410\n')
test_row_to_list.py:7: AssertionError
def test_for_missing_area():
> assert row_to_list("\t293,410\n") is None
E AssertionError: assert ['', '293,410'] is None
E + where ['', '293,410'] = row_to_list('\t293,410\n')
test_row_to_list.py:7: AssertionError
def test_for_missing_area():
> assert row_to_list("\t293,410\n") is None
E AssertionError: assert ['', '293,410'] is None
E + where ['', '293,410'] = row_to_list('\t293,410\n')
test_row_to_list.py:7: AssertionError
Result summary from all unit tests that ran: 1 failed, 2 passed tests.
Dibya Chakravorty
Test Automation Engineer
Unit tests serve as documentation
Test module: test_row_to_list.py
import pytest
import row_to_list
def test_for_clean_row():
assert row_to_list("2,081\t314,942\n") == \
["2,081", "314,942"]
def test_for_missing_area():
assert row_to_list("\t293,410\n") is None
def test_for_missing_tab():
assert row_to_list("1,463238,765\n") is None
def test_for_missing_area():
assert row_to_list("\t293,410\n") is None
def test_for_missing_tab():
assert row_to_list("1,463238,765\n") is None
Improved documentation.
More trust.
Reduced downtime.
convert_to_int()
convert_to_int()
Data
convert_to_int()
Data Feature
convert_to_int()
Data Feature
Models
convert_to_int()
Data Feature
Models
convert_to_int()
Data Feature
Models
convert_to_int()
Data Feature
Models