This is a basic example of a unit and then a number of minor variants.
We will test a function that caluculates the GC-content of a DNA sequence. The GC-content is simply the percentage of bases in the DNA sequence that are either G's or C's. So, for example, the GC-content of 'ATTGC' is 40%.
The function we are testing is get_gc_content()
and it takes a single
argument, which is a string represting a sequence. This function is in
a custom module called dna_analysis.py
.
dna_analysis
module¶%%file dna_analysis.py
"""Code for analyzing DNA sequences"""
from __future__ import division
def get_gc_content(seq):
"""Determine the GC content of a sequence"""
seq = seq.replace('\n', '').replace(' ', '').upper()
gc_content = 100 * (seq.count('G') + seq.count('C')) / len(seq)
return gc_content
Overwriting dna_analysis.py
checks to see if it returns the right value
%%file test_dna.py
from dna_analysis import get_gc_content
def test_get_gc_content_zero():
assert get_gc_content('ATTATTAAA') == 0
Overwriting test_dna.py
We would normally run nosetests from the command line, so in IPython we can just call !nosetests
.
!nosetests
. ---------------------------------------------------------------------- Ran 1 test in 0.015s OK
Fill in the blanks (indicated by _________) in the variations of this example below to achieve the desired test.
A. Test a sequence with 100% GC-content
%%file test_dna.py
from dna_analysis import get_gc_content
def test_get_gc_content_onehundred():
assert get_gc_content('GGGCCCGCGCG') == _____
B. Test a lower case sequence with 50% GC-content
%%file test_dna.py
from dna_analysis import get_gc_content
def test_get_gc_content_lowercase():
assert get_gc_content(___________) == 50
C. Test a mixed case sequence with 75% GC-content
%%file test_dna.py
from dna_analysis import get_gc_content
def test_get_gc_content_mixedcase():
assert get_gc_content(__________) == _____
D. Test a sequence with 25% GC-content
%%file test_dna.py
from dna_analysis import get_gc_content
def test_get_gc_content_twentyfive():
sequence = ______________
assert get_gc_content(____________) == _____
E. Test a sequence with 10% GC-content
%%file test_dna.py
from dna_analysis import get_gc_content
def ____________________():
____________ get_gc_content('AGTCAATTAT') == 10