 Python. 12    pytest
 


   ,   ,  ,    .     12   : , , ,  , ,  ,    .       ,   , ,  ,      .    Python 3.12  pytest 8.3.5,   -,     .    , ,     Python.      ,      .





 

 Python. 12    pytest





   





   


    .    ,     .     ,          .       ,         .         :  ,    .

    ,   ,     .           logic.py      test_logic.py  .       logic.py  .        .

   ,    ,   , ,    .            .   Python   .         :     .

    .         ;    ,   FB2,      .      ,     .         .




   


   ,  pytest-practice.       ,        .   Python 3.12  pytest  8.3.5.      ;   ,    .     Python 3.12.14.

   : python3.12 --version   Python 3.12.x.         python3.12 -m venv .venv.      ,    ,     .  Windows    py -3.12 -m venv .venv.  macOS/Linux    source .venv/bin/activate;  Windows PowerShell  .venv\Scripts\Activate.ps1.   PowerShell  ,     :   .venv\Scripts\python.exe       python.  macOS/Linux     .venv/bin/python.

   python -m pip install pytest==8.3.5,  python --version  python -m pytest --version.           pip ;      .       .  Python  pip  ,             .

 pytest-practice         lab01, lab02  .         .      pytest.py, pathlib.py  datetime.py:     .     UTF-8,      .   logic.py     .




    


         ,  001?.         ,     .   ,  Python.       ,       ,      .  .  .      .         .

           .    restore_listing.py    .       copied.txt,  UTF-8,   python restore_listing.py.        restored.py.          restored.py.    ,       logic.py, test_logic.py  test_extra.py    .      restored.py,    .

      ,   .         .        ,      :   ,   .                Python.         ;      .

  logic.py      ,  test_logic.py   .  python -m pytest -q test_logic.py      .    :    .          Python-.   SyntaxError, IndentationError, ImportError      ,         ,     .

       logic.py          .     .   ,    .    test_extra.py:      python -m pytest -q test_logic.py test_extra.py.     ,        .


restore_listing.py     

from pathlib import Path

raw = Path("copied.txt").read_text(encoding="utf-8")

rows = [s.strip() for s in raw.splitlines() if s.strip()]

assert rows and all("?" in s for s in rows)

parts = [s.split("?", 1) for s in rows]

assert [p[0] for p in parts] == [f"{i:03d}" for i in range(1, len(parts) + 1)]

code = "\n".join(p[1].replace("", " ") for p in parts) + "\n"

out = open("restored.py", "x", encoding="utf-8")

out.write(code)

out.close()




     


      ,      .     .     ,  , :           .      ,           .

  pytest  FAILED    ,   assert  ,   . ERROR,    collection,    : ,        .          ,   .  ,         .

 passed  ,    .   ,   ,          .     ,  :       ?          ,        .

        ;   tmp_path     .      ,         .        ,      ,     .        .




  


1.  :     (#case-1)

2.  :  ,    (#case-2)

3.   :    (#case-3)

4.  ,  :    (#litres_trial_promo)

5.  ,  :  fixture    (#litres_trial_promo)

6.   :  ,     (#litres_trial_promo)

7.  :      (#litres_trial_promo)

8. Stub:      (#litres_trial_promo)

9. Spy:     (#litres_trial_promo)

10. Monkeypatch:      (#litres_trial_promo)

11. :      (#litres_trial_promo)

12.  :      (#litres_trial_promo)




 





 1.  :    





   


    ,      .    :       .              ,       .


  

1. can_join(capacity, occupied)    int; bool      TypeError.

2. capacity   1; occupied   0  capacity .     ValueError.

3.   bool: True,       ;      False.

4.         .        .


  

    ,     .         ,  .   ,    .      :      .       , , ,    .

  :  ,   ,   .     ,       ,    .           :          .

 test_logic.py     .  can_join  ,  assert   .  is True  is False,      :      1    True.    ,       ,   .

   .       occupied <= capacity:       .      :          .       ,       .

     :   .  ,            .       True   False    .              False   .

       ,   .   :    .       ,         .     :    ,       .




logic.py     


001?defcan_join(capacity,occupied):

002?iftype(capacity)isnotintortype(occupied)isnotint:

003?raiseTypeError("capacityandoccupiedmustbeintegers")

004?ifcapacity<1ornot0<=occupied<=capacity:

005?raiseValueError("invalidcapacityoroccupancy")

006?returnoccupied<=capacity




test_logic.py    


001?fromlogicimportcan_join

002?

003?

004?deftest_empty_workshop_accepts_one_more():

005?assertcan_join(4,0)isTrue

006?

007?

008?deftest_one_free_place_accepts_one_more():

009?assertcan_join(4,3)isTrue

010?

011?

012?deftest_full_workshop_rejects_one_more():

013?assertcan_join(4,4)isFalse




,   


  lab01  python -m pytest -q test_logic.py.      3 :   1,   2.    ,       .

 test_full_workshop_rejects_one_more:        .        occupied == capacity  .    ,      .

  <=  <    .     :    False    .         ,    .




logic.py   


001?defcan_join(capacity,occupied):

002?iftype(capacity)isnotintortype(occupied)isnotint:

003?raiseTypeError("capacityandoccupiedmustbeintegers")

004?ifcapacity<1ornot0<=occupied<=capacity:

005?raiseValueError("invalidcapacityoroccupancy")

006?returnoccupied<capacity




    


   logic.py      .  3   .

     ,     .            .

       .             .       :            .         .

   1 (#litres_trial_promo)

   (#navigation)




 2.  :  ,   





   


          .         pytest.mark.parametrize.       ,                 .


  

1. talk_band(minutes)    int,  bool;    TypeError.

2.   minutes  0  120 .        .     ValueError.

3.  05      'short';  620  'regular';  21120  'long'.

4.         .     .


  

     .        : -    ,         regular.     ,     .    ,       .

    0  5,    6, 19  20,    21  120.  5/6  20/21       .  19            .  0  120      ;        .

   , minutes  expected,     . pytest        .    test_talk_band    .      :             .

 expected    .         if,        .     :           .      ,        .

         ,     .       20,  19  21 ,      regular ? long.         .     ,       if:   .

        :    ,     .    xfail   :       ,   .      ,  ,        .




logic.py     


001?deftalk_band(minutes):

002?iftype(minutes)isnotint:

003?raiseTypeError("minutesmustbeaninteger")

004?ifnot0<=minutes<=120:

005?raiseValueError("minutesmustbebetween0and120")

006?ifminutes<=5:

007?return"short"

008?ifminutes<20:

009?return"regular"

010?return"long"




test_logic.py    


001?importpytest

002?

003?fromlogicimporttalk_band

004?

005?

006?@pytest.mark.parametrize(

007?"minutes,expected",

008?[

009?(0,"short"),

010?(5,"short"),

011?(6,"regular"),

012?(19,"regular"),

013?(20,"regular"),

014?(21,"long"),

015?(120,"long"),

016?],

017?)

018?deftest_talk_band(minutes,expected):

019?asserttalk_band(minutes)==expected




,   


  lab02  python -m pytest -q test_logic.py.      7 :   1,   6.    ,       .

    : minutes == 20  'long'  'regular'.  if    ,   minutes < 20   .          .          .

    minutes <= 20.   regular    ,     5      .  5  6    ,     .        .




logic.py   


001?deftalk_band(minutes):

002?iftype(minutes)isnotint:

003?raiseTypeError("minutesmustbeaninteger")

004?ifnot0<=minutes<=120:

005?raiseValueError("minutesmustbebetween0and120")

006?ifminutes<=5:

007?return"short"

008?ifminutes<=20:

009?return"regular"

010?return"long"




    


   logic.py      .  7   .

      : 3, 12  70 .        ,   .

   ,     .     121         .    ,       :            .

   2 (#litres_trial_promo)

   (#navigation)




 3.   :   





   


       .          .     pytest.raises:          ,        .


  

1. require_batch_size(size)    int  1  50       .

2.         ValueError,    Exception   .

3.   ,  bool, float, str  None,     TypeError.   .

4.     .     :   ,  .


  

      .  0   ,   ;  '12'   ,        .        .                 .

   : 1, 12  50   .         .  ,      ,      .          ,    .

 0  51  with pytest.raises(ValueError).         .     ,   ;   Exception       ,    .       ,           .




  .


   .

   ,     (https://www.litres.ru/book/raznoe/testiruem-python-12-laboratornykh-rabot-s-pytest-74417689/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


