ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • pytest - fixture
    python/응용 2022. 5. 23. 17:20

    기본 사용

    import pytest
    
    
    @pytest.fixture()
    def setup():
        print("\nSetup ===============")
    
    
    def test1(setup):
        print("Execute test1")
        assert True
    test_cal.py::test1 
    Setup ===============
    PASSED                                                [100%]Execute test1

     

    yield 사용

    import pytest
    
    
    @pytest.fixture()
    def setup2():
        print("\nBefore ====================")
        yield
        print("\nAfter =====================")
    
    
    def test2(setup2):
        print("Execute test2")
        assert True
    test_cal.py::test2 
    Before ====================
    PASSED                                                [100%]Execute test2
    
    After =====================

     

     

    params 사용

    import pytest
    
    
    @pytest.fixture(params=[1, 2, 3])
    def setup3(request):
        ret = request.param
        print(f"\nret={ret}")
        return ret
    
    
    def test3(setup3):
        print(f"\nsetup3 = {setup3}")
        assert True
    test_cal.py::test3[1] 
    ret=1
    PASSED                                             [ 33%]
    setup3 = 1
    
    test_cal.py::test3[2] 
    ret=2
    PASSED                                             [ 66%]
    setup3 = 2
    
    test_cal.py::test3[3] 
    ret=3
    PASSED                                             [100%]
    setup3 = 3

     

    skip slow test

    pip install pytest-skip-slow

     

    • 코드 작성
    import time
    import pytest
    
    
    def my_slow_func():
        time.sleep(5)
        return True
    
    @pytest.mark.slow
    def test_my_slow_func():
        assert my_slow_func()
    • 실행
      • slow test 제외(default): pytest -v test_slow.py
      • slow test 포함: pytest -v --slow test_slow.py
    • 참고

     

    'python > 응용' 카테고리의 다른 글

    requests  (0) 2022.05.31
    json 읽기 / 쓰기  (0) 2022.05.31
    pytest - basic  (0) 2022.05.23
    Thread - queue & PoisonPill  (0) 2022.05.23
    logging  (0) 2022.05.22

    댓글

Designed by Tistory.