You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Chapter5/testing.ipynb
+37-10Lines changed: 37 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -2308,7 +2308,7 @@
2308
2308
"id": "915e9960",
2309
2309
"metadata": {},
2310
2310
"source": [
2311
-
"### FreezeGun: Freeze Dynamic Time in Unit Testing "
2311
+
"### FreezeGun: Freezing Time for Reliable Python Testing"
2312
2312
]
2313
2313
},
2314
2314
{
@@ -2325,21 +2325,37 @@
2325
2325
"!pip install freezegun"
2326
2326
]
2327
2327
},
2328
+
{
2329
+
"cell_type": "markdown",
2330
+
"id": "d979b680-a898-402b-b92d-412cfbb3572c",
2331
+
"metadata": {},
2332
+
"source": [
2333
+
"Testing time-dependent functions can be challenging and unreliable as the results may vary based on when the test is executed. This results in flaky tests that pass or fail inconsistently."
2334
+
]
2335
+
},
2328
2336
{
2329
2337
"attachments": {},
2330
2338
"cell_type": "markdown",
2331
2339
"id": "85ef39bd",
2332
2340
"metadata": {},
2333
2341
"source": [
2334
-
"Unit tests require static input, but time is dynamic and constantly changing. With FreezeGun, you can freeze time to a specific point, ensuring accurate verification of the tested features."
2342
+
"With FreezeGun, you can freeze time at a particular point, ensuring your tests always run with the same date context."
2335
2343
]
2336
2344
},
2337
2345
{
2338
2346
"cell_type": "code",
2339
-
"execution_count": null,
2347
+
"execution_count": 6,
2340
2348
"id": "62213cd9",
2341
2349
"metadata": {},
2342
-
"outputs": [],
2350
+
"outputs": [
2351
+
{
2352
+
"name": "stdout",
2353
+
"output_type": "stream",
2354
+
"text": [
2355
+
"Overwriting test_freezegun.py\n"
2356
+
]
2357
+
}
2358
+
],
2343
2359
"source": [
2344
2360
"%%writefile test_freezegun.py\n",
2345
2361
"from freezegun import freeze_time\n",
@@ -2348,9 +2364,9 @@
2348
2364
"def get_day_of_week():\n",
2349
2365
" return datetime.datetime.now().weekday()\n",
2350
2366
"\n",
2351
-
"@freeze_time(\"2023-06-13\")\n",
2367
+
"@freeze_time(\"2024-10-13\")\n",
2352
2368
"def test_get_day_of_week():\n",
2353
-
" assert get_day_of_week() == 1\n"
2369
+
" assert get_day_of_week() == 6\n"
2354
2370
]
2355
2371
},
2356
2372
{
@@ -2366,7 +2382,7 @@
2366
2382
},
2367
2383
{
2368
2384
"cell_type": "code",
2369
-
"execution_count": 16,
2385
+
"execution_count": 7,
2370
2386
"id": "376870d5",
2371
2387
"metadata": {
2372
2388
"tags": [
@@ -2379,21 +2395,32 @@
2379
2395
"output_type": "stream",
2380
2396
"text": [
2381
2397
"\u001b[1m============================= test session starts ==============================\u001b[0m\n",
2382
-
"platform darwin -- Python 3.9.6, pytest-7.2.1, pluggy-1.0.0\n",
2398
+
"platform darwin -- Python 3.11.6, pytest-8.2.0, pluggy-1.5.0\n",
2399
+
"Fugue tests will be initialized with options:\n",
"\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.03s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
2406
+
"\u001b[32m============================== \u001b[32m\u001b[1m1 passed\u001b[0m\u001b[32m in 0.07s\u001b[0m\u001b[32m ===============================\u001b[0m\n"
2390
2407
]
2391
2408
}
2392
2409
],
2393
2410
"source": [
2394
2411
"!pytest test_freezegun.py"
2395
2412
]
2396
2413
},
2414
+
{
2415
+
"cell_type": "markdown",
2416
+
"id": "9274e666-b811-4997-9764-117ec5257f90",
2417
+
"metadata": {},
2418
+
"source": [
2419
+
"This code uses `get_day_of_week()` to return the current weekday (0-6). The `@freeze_time(\"2024-10-13\")` decorator sets a fixed date (Sunday, October 13, 2024).\n",
2420
+
"\n",
2421
+
"The test calls `get_day_of_week()` and checks if it returns 6 (Sunday). This test will consistently pass because FreezeGun ensures `datetime.datetime.now()` always returns the specified frozen date."
Copy file name to clipboardExpand all lines: docs/Chapter5/testing.html
+19-10Lines changed: 19 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -531,7 +531,7 @@ <h2> Contents </h2>
531
531
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-steps-share-data-between-tests">6.13.16. pytest-steps: Share Data Between Tests</a></li>
532
532
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-picked-run-the-tests-related-to-the-unstaged-files-in-git">6.13.17. pytest-picked: Run the Tests Related to the Unstaged Files in Git</a></li>
533
533
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#efficient-testing-of-python-class-with-setup-method">6.13.18. Efficient Testing of Python Class with setUp Method</a></li>
534
-
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#freezegun-freeze-dynamic-time-in-unit-testing">6.13.19. FreezeGun: Freeze Dynamic Time in Unit Testing</a></li>
534
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#freezegun-freezing-time-for-reliable-python-testing">6.13.19. FreezeGun: Freezing Time for Reliable Python Testing</a></li>
535
535
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#simulate-external-services-in-testing-with-mock-objects">6.13.20. Simulate External Services in Testing with Mock Objects</a></li>
536
536
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-mock-vs-unittest-mock-simplifying-mocking-in-python-tests">6.13.21. pytest-mock vs unittest.mock: Simplifying Mocking in Python Tests</a></li>
537
537
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#tmp-path-create-a-temporary-directory-for-testing">6.13.22. tmp_path: Create a Temporary Directory for Testing</a></li>
<h2><spanclass="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time in Unit Testing<aclass="headerlink" href="#freezegun-freeze-dynamic-time-in-unit-testing" title="Permalink to this heading">#</a></h2>
<h2><spanclass="section-number">6.13.19. </span>FreezeGun: Freezing Time for Reliable Python Testing<aclass="headerlink" href="#freezegun-freezing-time-for-reliable-python-testing" title="Permalink to this heading">#</a></h2>
@@ -1773,7 +1773,8 @@ <h2><span class="section-number">6.13.19. </span>FreezeGun: Freeze Dynamic Time
1773
1773
</div>
1774
1774
</details>
1775
1775
</div>
1776
-
<p>Unit tests require static input, but time is dynamic and constantly changing. With FreezeGun, you can freeze time to a specific point, ensuring accurate verification of the tested features.</p>
1776
+
<p>Testing time-dependent functions can be challenging and unreliable as the results may vary based on when the test is executed. This results in flaky tests that pass or fail inconsistently.</p>
1777
+
<p>With FreezeGun, you can freeze time at a particular point, ensuring your tests always run with the same date context.</p>
<p>This code uses <codeclass="docutils literal notranslate"><spanclass="pre">get_day_of_week()</span></code> to return the current weekday (0-6). The <codeclass="docutils literal notranslate"><spanclass="pre">@freeze_time("2024-10-13")</span></code> decorator sets a fixed date (Sunday, October 13, 2024).</p>
1819
+
<p>The test calls <codeclass="docutils literal notranslate"><spanclass="pre">get_day_of_week()</span></code> and checks if it returns 6 (Sunday). This test will consistently pass because FreezeGun ensures <codeclass="docutils literal notranslate"><spanclass="pre">datetime.datetime.now()</span></code> always returns the specified frozen date.</p>
1811
1820
<p><aclass="reference external" href="https://github.com/spulec/freezegun">Link to FreezeGun</a>.</p>
@@ -3235,7 +3244,7 @@ <h2><span class="section-number">6.13.32. </span>DeepEval: Unit Testing for Your
3235
3244
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-steps-share-data-between-tests">6.13.16. pytest-steps: Share Data Between Tests</a></li>
3236
3245
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-picked-run-the-tests-related-to-the-unstaged-files-in-git">6.13.17. pytest-picked: Run the Tests Related to the Unstaged Files in Git</a></li>
3237
3246
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#efficient-testing-of-python-class-with-setup-method">6.13.18. Efficient Testing of Python Class with setUp Method</a></li>
3238
-
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#freezegun-freeze-dynamic-time-in-unit-testing">6.13.19. FreezeGun: Freeze Dynamic Time in Unit Testing</a></li>
3247
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#freezegun-freezing-time-for-reliable-python-testing">6.13.19. FreezeGun: Freezing Time for Reliable Python Testing</a></li>
3239
3248
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#simulate-external-services-in-testing-with-mock-objects">6.13.20. Simulate External Services in Testing with Mock Objects</a></li>
3240
3249
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#pytest-mock-vs-unittest-mock-simplifying-mocking-in-python-tests">6.13.21. pytest-mock vs unittest.mock: Simplifying Mocking in Python Tests</a></li>
3241
3250
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#tmp-path-create-a-temporary-directory-for-testing">6.13.22. tmp_path: Create a Temporary Directory for Testing</a></li>
Copy file name to clipboardExpand all lines: docs/README.html
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -482,7 +482,7 @@ <h2> Contents </h2>
482
482
<navaria-label="Page">
483
483
<ulclass="visible nav section-nav flex-column">
484
484
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#">What Should You Expect From This Book?</a></li>
485
-
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#about-this-book">About This Book</a></li>
485
+
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#printable-pdf-guide">Printable PDF Guide</a></li>
486
486
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#about-the-author">About The Author</a></li>
487
487
</ul>
488
488
@@ -519,9 +519,9 @@ <h1>What Should You Expect From This Book?<a class="headerlink" href="#what-shou
519
519
<p>This book expects you to have some basic knowledge of Python and data science.</p>
520
520
<p>You should also expect bite-size code snippets for each section. This will allow you to obtain multiple pieces of knowledge in fewer than one minute. I included the link to the resources for every tools introduced in case you want to explore them further.</p>
521
521
</section>
522
-
<sectionid="about-this-book">
523
-
<h1>About This Book<aclass="headerlink" href="#about-this-book" title="Permalink to this heading">#</a></h1>
524
-
<p>This book includes more than 800 tips and tools I have shared daily on my website,<aclass="reference external" href="https://codecut.ai/?utm_source=github&utm_medium=efficient_python_tricks&utm_campaign=about_this_book">CodeCut</a>. If you want to get the updated of new tips on your mailbox, you can subscribe to <aclass="reference external" href="https://codecut.ai/daily-tips-form/?utm_source=github&utm_medium=efficient_python_tricks&utm_campaign=subscribe_to_newsletter">my newsletter</a>.</p>
522
+
<sectionid="printable-pdf-guide">
523
+
<h1>Printable PDF Guide<aclass="headerlink" href="#printable-pdf-guide" title="Permalink to this heading">#</a></h1>
524
+
<p>For a printer-friendly version of the tools mentioned in this book, sign up for<aclass="reference external" href="https://codecut.ai/data-scientist-toolkit/?utm_source=github&utm_medium=defficient_python_tricks&utm_campaign=free_pdf">CodeCut’s free PDF guide</a>. This comprehensive 264-page document covers over 100 essential data science tools, providing you with a valuable reference that you can print and keep at your desk.</p>
525
525
</section>
526
526
<sectionid="about-the-author">
527
527
<h1>About The Author<aclass="headerlink" href="#about-the-author" title="Permalink to this heading">#</a></h1>
@@ -586,7 +586,7 @@ <h1>About The Author<a class="headerlink" href="#about-the-author" title="Permal
586
586
<navclass="bd-toc-nav page-toc">
587
587
<ulclass="visible nav section-nav flex-column">
588
588
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#">What Should You Expect From This Book?</a></li>
589
-
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#about-this-book">About This Book</a></li>
589
+
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#printable-pdf-guide">Printable PDF Guide</a></li>
590
590
<liclass="toc-h1 nav-item toc-entry"><aclass="reference internal nav-link" href="#about-the-author">About The Author</a></li>
0 commit comments