|
4 | 4 |  |
5 | 5 |  |
6 | 6 |
|
7 | | -A simple API for unit testing a Python project's performances :zap::wheelchair:. |
| 7 | +A simple and lightweight API for unit testing a Python 🐍 project's performances :zap::wheelchair:. |
| 8 | + |
| 9 | +From **Python 3.5 to 3.8**, easily improve your project's performances tests through the use of dedicated **decorators**. |
| 10 | + |
| 11 | +### Table of contents |
| 12 | + |
| 13 | +- [Memory testing](#Memory-testing) |
| 14 | + - [Memory overload assertions](#Memory-overload-assertions) |
| 15 | + - [Memory leaks assertions](#Memory-leaks-assertions) |
| 16 | + |
| 17 | +### Memory testing |
| 18 | + |
| 19 | +#### Memory overload assertions |
| 20 | + |
| 21 | +The memory overload assertions are possible thanks to the `@memory_not_exceed` decorator. |
| 22 | + |
| 23 | +A simple *TestCase* associated to this decorator does the job : |
| 24 | + |
| 25 | +```python |
| 26 | +class TestLimitMemoryUsage(unittest.TestCase): |
| 27 | + """ |
| 28 | + This class illustrates the use of the memory_not_exceed decorator. |
| 29 | + """ |
| 30 | + @memory_not_exceed(threshold=0) |
| 31 | + def test_memory_usage_exceed(self): |
| 32 | + """ |
| 33 | + This test won't pass due to a very low threshold. |
| 34 | + """ |
| 35 | + return list(range(1000)) * 1 |
| 36 | + |
| 37 | + @memory_not_exceed(threshold=1000) |
| 38 | + def test_memory_usage_not_exceed(self): |
| 39 | + """ |
| 40 | + This test passes due to a very high threshold. |
| 41 | + """ |
| 42 | + return list(range(1000)) * 1 |
| 43 | +``` |
| 44 | + |
| 45 | +#### Memory leaks assertions |
| 46 | + |
| 47 | +The memory leaks assertions are possible thanks to the `@memory_not_leak` decorator. |
| 48 | + |
| 49 | +Once again, a simple *TestCase* associated to this decorator does the job : |
| 50 | + |
| 51 | +```python |
| 52 | +class TetsMemoryLeaksDetection(unittest.TestCase): |
| 53 | + """ |
| 54 | + This class illustrates the use of the memory_not_leak decorator. |
| 55 | + """ |
| 56 | + leaking_list = [] |
| 57 | + |
| 58 | + def leak(self): |
| 59 | + """ |
| 60 | + Generates a memory leak involving the leaking_list. |
| 61 | + """ |
| 62 | + self.leaking_list.append("will leak") |
| 63 | + |
| 64 | + @memory_not_leak() |
| 65 | + def test_memory_leaks(self): |
| 66 | + """ |
| 67 | + This test won't pass due to a memory leak. |
| 68 | + """ |
| 69 | + self.leak() |
| 70 | + |
| 71 | + @memory_not_leak() |
| 72 | + def test_memory_not_leak(self): |
| 73 | + """ |
| 74 | + This test passes due to the absence of memory leak. |
| 75 | + """ |
| 76 | + valid_list = [] |
| 77 | + valid_list.append("won't leak") |
| 78 | +``` |
0 commit comments