Some benefits of practicing TDD include:
- Immediate feedback on if your code is working, so you can find bugs faster
- Confidence in refactoring
- Helps define how the code should be used
- Encourages writing only the necessary code required
- Writing tests before writing code
- Write the minimum amount of code necessary to make tests pass
- Refactor your code where necessary
- Rinse and repeat
Red-green-refactor is a framework that developers use to build a test suite, write implementation code, and optimize their codebase in short development cycles.
- Write a test
- Run the test and see your test fail for the right reason (Red)
- Write the minimum amount of code necessary to make the tests pass
- Run the tests, and see that all tests should pass (Green)
Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass.
In python, there are many testing frameworks available. Unittest and Pytest are the more commonly used ones. Feel free to use whichever test framework you are comfortable with. However the code examples provided by us will be in unittest.
You are given strings of different lengths. If the number of vowels are more than 30% of the string length then insert ‘mummy’ for each continuous set of vowels.
For example <input>
-> <output>
,
- his -> hmummys
- hear -> hmummyr
The challenge for you today is to complete the above problem with a TDD (Red-Green-Refactor) approach.
- Python 3 installed
- Any text editor of your choice
- Basic knowledge of Python
- Understand the requirements and have a plan of what scenarios you wish to test for.
- Hint: The key criteria have been highlighted in bold. Test for scenarios that fulfill and don't fulfill the criteria to be "mummified"
- Plan the list of inputs and their expected outputs for each of the test scenarios you can think of.
- Follow the steps of Red-Green-Refactor and finish implementing the solution.
- The first test method has been set up for you. You may choose to clone the repo or start from scratch by yourself. (It's easy!)
- The tests are written in
test_string_mummifier.py
while the implementation is written instring_mummifier.py
- The tests are written in
- You can run all tests with this command while in the project directory:
python3 -m unittest test_string_mummifier.py
. Or if you are using an IDE like PyCharm, you should be able to run tests from the IDE itself by right clicking the test name and selecting 'Run unittests for ...' - If you are unfamiliar with python, we recommend that you start from referring to our step by step guide here
- The first test method has been set up for you. You may choose to clone the repo or start from scratch by yourself. (It's easy!)
- Repeat the Red-Green-Refactor steps till your functionality is complete.
- If you are stuck with coming up with a list of test scenarios, you can refer to this link to see the complete list we have come up with.
- Strive to give each of your test methods a meaningful name describing the scenario you are testing and its expected behaviour if possible.
- If your code has conditional branching involved, do ensure that each possible execution path is covered by at least one test.
- Finally, have fun!
- The sample solution can be found by checking out the
final-solution
branch in this repository. - If you’re interested in reading more about TDD, here are some useful books:
We'd sure love to hear what you think about this workshop! Just fill in this form and share your thoughts!