Commit 8b276afe authored by Vladimir Paun's avatar Vladimir Paun
Browse files


parent eb5b110a
This is an exercise used to learn Test-Driven Development and unit testing in Python.
#MO101 - Test-Driven Development and unit testing in Python with git flavour.
## Rationale
Binary numbers are rather easy to understand, even if becoming familiar with them requires some time.
......@@ -71,13 +71,193 @@ Simple tasks are the best way to try and use new development methodologies, so t
* **TODO list**: until all tests run successfully you have something still waiting to be implemented.
## Writing some tests
So now we will pretend we have already developed our Binary class and write some tests that check its behaviour. You will find the whole file in the Resources section at the end of the post, I will show here just some snippets.
## Randomness
The `random` module is tested through the properties of the output it generates (it's not test-driven as it is a standard module provided by the runtime.) For example, sampled elements from a list must belong to the list while shuffling a list should not lose any contained element.
Run with:
import unittest
#define Test class as a derived class of the unittest.TestCase base class
class Test(unittest.TestCase):
def setUp(self):
def test_init(self):
## Runing the test
You can use the `-m unittest` option when interpreting scripts when extending the base class `unittest.TestCase`. Eitherwise you can always add a `main` block and call inside all the test methods/functions.
You may find the documentation for unit testing [here](
import unittest
from binary import Binary
#define Test class as a derived class of the unittest.TestCase base class
class Test(unittest.TestCase):
def setUp(self):
self.binary = Binary(6)
def test_binary_init_int(self):
binary = Binary(6)
assert int(binary) == 6
### From the command line use the unittest command to run your tests:
python3 -m unittest test_random
## Git Command line instructions
These instructions are present on the gitLab site also after creating a new project.
##### Git global setup
git config --global "Paun Vladimir Alexandru"
git config --global ""
##### Create a new repository
mkdir test_proj
cd test_proj
git init
git add
git commit -m "first commit"
git remote add origin
git push -u origin master
##### Push an existing Git repository
cd existing_git_repo
git remote add origin
git push -u origin master
## Project management
For this project you will work in teams of two. Most of the code will be written togather, as a group. One person will write the production code but both decide the content.
### Roles
One person from the team will be responsible with the production of the test, following the TDD approach. Test are writen before the production code, write just enaugh code to validate the last added code.
1. [R1] - writing the tests
2. [R2] - writing production code to validate the tests
### Workflow
For this assignment you will work in teams of two on separate computers moat of the time. Every time a team member pushes results on the git repository, the other member will retreive the modifications, add just enaugh code to validate the added test(s) and pushes the results.
Create the test class and a first test method (R1). Add/commit the source to the git and push when it's ready. Get the updates and write the code needed to validate the new test cases (R2).
### Q1 Create a gitLab account
The ENSTA [gitLab]( address.
Please follow the intructions provided on the website.
### Q2 Create a new gitLab project
Chose a name and create a new gitLab project.
### Q3 Access the provided code from the git repository
Git repository [address](
### Q4 Initialization
First of all we import the class from the `` file (which doesn't exists yet). The `test_binary_init_int()` function shall (as the name suggests) initialize a `Binary` with an integer. The assertion checks that the newly created binary variable has a consistent integer representation, which is the number we used to initialize it.
### Q5 Test all the initialisations one by one
We want to be able to initialize a Binary with a wide range of values: bit strings (`'110'`), binary strings (`'0b110'`), hexadecimal strings (`'0x6'`), hexadecimal values (`0x6`), lists of integers (`[1,1,0]`) and list of strings (`['1','1','0']`). The following tests check all those cases, please add them progresively in order to practice the commit/push/pull cycles.
* `test_binary_init_bitstr()`
* `test_binary_init_binstr()`
* `test_binary_init_hexstr()`
* `test_binary_init_hex() `
* `test_binary_init_intseq()`
* `test_binary_init_strseq()`
### Q6 Negative numbers
Let us now check that our `Binary` class cannot be initialized with a negative number. So for simple binaries we just discard negative numbers.
### Q7 Conversions
We want to check that my binary numbers can be correctly converted to integers (through `int()`), binary strings (through `bin()`), hexadecimals (through `hex()`) and to strings (through `str()`). We want the string representation to be a plain sequence of zeros and ones, that is the binary string representation without the `0b` prefix.
### Q8 Binary operations
Now it is time to add new features to our Binary class. As already said, the TDD methodology wants us to first write the tests, then to write the code. Our class is missing some basic arithmetic and binary operations so the tests are
python -m unittest test_random
\ No newline at end of file
Have a look [here]( for the syntax used in python to override operators.
### Q9 Slicing
We want Binary to support slicing, just like lists. The difference between lists and my Binary type is that for the latter indexes start from the rightmost element. So when we get bits 3:7 of an 8-bit Binary we obtain the four leftmost ones. The desired behaviour is thus exemplified by
``` python
>>> b = Binary('01101010')
>>> b[4:7]
<binary.Binary object at 0x...> (110)
>>> b[1:3]
<binary.Binary object at 0x...> (101)
### Q10 Splitting binaries
The last feature we want to add is a split() function that divides the binary number in two binaries. The rightmost one shall have the given size in bits, while the leftmost just contains the remaining bits.
## Resources
1. [git-cheat-sheet](
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment