
عنوان:
Python Testing with pytest
نویسنده:
Brian Okken
انتشارات:
Pragmatic Bookshelf
تاریخ انتشار:
2022
حجم:
MB
معرفی کتاب:"تست پایتون با pytest: ساده، سریع، موثر و مقیاسپذیر ویرایش دوم "
توضیحات کتاب:
برنامهها، پکیجها و کتابخانهها، کوچک و بزرگ، را با pytest، قدرتمندترین فریمورک تست Python، تست کنید. pytest به شما کمک میکند تستها را سریع بنویسید و آنها را خوانا و قابل نگهداری نگه دارید. در این نسخه بازبینیشده، قابلیتهای فوقالعاده pytest شامل assert ساده، fixtures، پارامتریسازی، markers و plugins را بررسی میکنید و همزمان تستها و مجموعه تستها را روی یک برنامه کوچک پایگاه داده ایجاد میکنید. با استفاده از مدل fixture قوی ولی ساده، نوشتن تستهای کوچک با pytest به همان اندازه آسان است که به تستهای عملکردی پیچیده مقیاس دهید. این کتاب به شما نشان میدهد چگونه این کار را انجام دهید.
pytest بدون شک بهترین انتخاب برای تست پروژههای Python است. این فریمورک کامل، منعطف و قابل توسعه است. مدل fixture در pytest اجازه میدهد دادههای تست و فرآیندهای راهاندازی را در چندین لایه تست به اشتراک بگذارید. فریمورک pytest ویژگیهای قدرتمندی مانند بازنویسی assert، پارامتریسازی، markers، plugins، اجرای تست موازی و گزارش واضح شکست تست — بدون هیچ کد boilerplate — ارائه میدهد.
با دستورالعملهای ساده مرحلهبهمرحله و نمونه کد، این کتاب شما را به سرعت با این ابزار قدرتمند و آسان برای یادگیری آشنا میکند. تستهای کوتاه و قابل نگهداری بنویسید که بهطور شفاف نشان دهند چه چیزی را تست میکنید. زمان تست را با توزیع تستها روی چند پردازنده و اجرای موازی آنها کاهش دهید. از assertهای داخلی Python به جای توابع کمکی assert استفاده کنید تا تستها خواناتر شوند. کد راهاندازی را از تستها خارج کرده و در fixtures قرار دهید تا خطاهای راهاندازی از خطاهای تست جدا شوند. شرایط خطا و موارد مرزی را با تست استثناهای مورد انتظار بررسی کنید و با پارامتریسازی یک تست، چندین مورد را اجرا کنید. pytest را با plugins گسترش دهید، آن را به سیستمهای CI متصل کنید و همزمان با tox، mock، coverage و حتی تستهای موجود unittest استفاده کنید.
آنچه خواهید آموخت:
- نوشتن تستهای ساده و قابل نگهداری با pytest به سرعت
- استفاده از fixtures برای مدیریت راهاندازی و دادههای تست
- اجرای تستها به صورت موازی و مدیریت تستهای پیچیده
آنچه نیاز دارید:
نمونهها در این کتاب با Python 3.10 و pytest 7 نوشته شدهاند. pytest 7 از Python 3.5 به بالا پشتیبانی میکند.
فهرست مطالب
- Acknowledgments
- Preface
- Part I: Primary Power
- Chapter 1: Getting Started with pytest
- Installing pytest
- Running pytest
- Review
- Exercises
- What’s Next
- Chapter 2: Writing Test Functions
- Installing the Sample Application
- Writing Knowledge-Building Tests
- Using assert Statements
- Failing with pytest.fail() and Exceptions
- Writing Assertion Helper Functions
- Testing for Expected Exceptions
- Structuring Test Functions
- Grouping Tests with Classes
- Running a Subset of Tests
- Review
- Exercises
- What’s Next
- Chapter 3: pytest Fixtures
- Getting Started with Fixtures
- Using Fixtures for Setup and Teardown
- Tracing Fixture Execution with –setup-show
- Specifying Fixture Scope
- Sharing Fixtures through conftest.py
- Finding Where Fixtures Are Defined
- Using Multiple Fixture Levels
- Using Multiple Fixtures per Test or Fixture
- Deciding Fixture Scope Dynamically
- Using autouse for Fixtures That Always Get Used
- Renaming Fixtures
- Review
- Exercises
- What’s Next
- Chapter 4: Builtin Fixtures
- Using tmp_path and tmp_path_factory
- Using capsys
- Using monkeypatch
- Remaining Builtin Fixtures
- Review
- Exercises
- What’s Next
- Chapter 5: Parametrization
- Testing Without Parametrize
- Parametrizing Functions
- Parametrizing Fixtures
- Parametrizing with pytest_generate_tests
- Using Keywords to Select Test Cases
- Review
- Exercises
- What’s Next
- Chapter 6: Markers
- Using Builtin Markers
- Skipping Tests with pytest.mark.skip
- Skipping Tests Conditionally with pytest.mark.skipif
- Expecting Tests to Fail with pytest.mark.xfail
- Selecting Tests with Custom Markers
- Marking Files, Classes, and Parameters
- Using “and,” “or,” “not,” and Parentheses with Markers
- Being Strict with Markers
- Combining Markers with Fixtures
- Listing Markers
- Review
- Exercises
- What’s Next
- Part II: Working with Projects
- Chapter 7: Strategy
- Determining Test Scope
- Considering Software Architecture
- Evaluating the Features to Test
- Creating Test Cases
- Writing a Test Strategy
- Review
- Exercises
- What’s Next
- Chapter 8: Configuration Files
- Understanding pytest Configuration Files
- Saving Settings and Flags in pytest.ini
- Using tox.ini, pyproject.toml, or setup.cfg in place of pytest.ini
- Determining a Root Directory and Config File
- Sharing Local Fixtures and Hook Functions with conftest.py
- Avoiding Test File Name Collision
- Review
- Exercises
- What’s Next
- Chapter 9: Coverage
- Using coverage.py with pytest-cov
- Generating HTML Reports
- Excluding Code from Coverage
- Running Coverage on Tests
- Running Coverage on a Directory
- Running Coverage on a Single File
- Review
- Exercises
- What’s Next
- Chapter 10: Mocking
- Isolating the Command-Line Interface
- Testing with Typer
- Mocking an Attribute
- Mocking a Class and Methods
- Keeping Mock and Implementation in Sync with Autospec
- Making Sure Functions Are Called Correctly
- Creating Error Conditions
- Testing at Multiple Layers to Avoid Mocking
- Using Plugins to Assist Mocking
- Review
- Exercises
- What’s Next
- Chapter 11: tox and Continuous Integration
- What Is Continuous Integration?
- Introducing tox
- Setting Up tox
- Running tox
- Testing Multiple Python Versions
- Running tox Environments in Parallel
- Adding a Coverage Report to tox
- Specifying a Minimum Coverage Level
- Passing pytest Parameters Through tox
- Running tox with GitHub Actions
- Review
- Exercises
- What’s Next
- Chapter 12: Testing Scripts and Applications
- Testing a Simple Python Script
- Testing an Importable Python Script
- Separating Code into src and tests Directories
- Defining the Python Search Path
- Testing requirements.txt-Based Applications
- Review
- Exercises
- What’s Next
- Chapter 13: Debugging Test Failures
- Adding a New Feature to the Cards Project
- Installing Cards in Editable Mode
- Debugging with pytest Flags
- Re-Running Failed Tests
- Debugging with pdb
- Combining pdb and tox
- Review
- Exercises
- What’s Next
- Part III: Booster Rockets
- Chapter 14: Third-Party Plugins
- Finding Plugins
- Installing Plugins
- Exploring the Diversity of pytest Plugins
- Running Tests in Parallel
- Randomizing Test Order
- Review
- Exercises
- What’s Next
- Chapter 15: Building Plugins
- Starting with a Cool Idea
- Building a Local conftest Plugin
- Creating an Installable Plugin
- Testing Plugins with pytester
- Testing Multiple Python and pytest Versions with tox
- Publishing Plugins
- Review
- Exercises
- What’s Next
- Chapter 16: Advanced Parametrization
- Using Complex Values
- Creating Custom Identifiers
- Parametrizing with Dynamic Values
- Using Multiple Parameters
- Using Indirect Parametrization
- Review
- Exercises
- What’s Next
- A1. Virtual Environments
- A2. pip
مشخصات
نام کتاب
Python Testing with pytest
نویسنده
Brian Okken
انتشارات
Pragmatic Bookshelf
تاریخ انتشار
2022
ISBN
9781680508604
تعداد صفحات
410
زبان
انگلیسی
فرمت
حجم
3.7MB
موضوع
Python