Skip to content

Pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

Commandline optionas

-k EXPRESSION
expression에 해당하는 테스트만 진행한다. 테스트 케이스 클래스, 함수명 등을 사용할 수 있다.
The matching is case-insensitive.
-m MARKEXPR
only run tests matching given mark expression.
--capture=method
캡쳐 방식을 선택한다. fd, sys, no, tee-sys 중 하나를 선택.
-s
--capture=no 와 동등하다. 테스트중 출력되지 않는 stdout 등을 출력되게 하고 싶다면 이 옵션을 사용하면 된다.

assertEqual vs assertSame

  • assertEqual는 두 값이 같은지를 비교하는 단정문이고
  • assertSame은 두 객체가 동일한 객체인지 주소값으로 비교하는 단정문이다.

assertSame싱글톤 (Singleton) 패턴으로 만들어진 객체를 비교확인하기도 하고 리스트의 clone메소드로 복사된 객체를 비교할 수도 있다.

assertEquals vs assertEqual

Actually, in Python 2.6, both assertEqual and assertEquals are convenience aliases to failUnlessEqual. The source declares them thus:

# Synonyms for assertion methods
assertEqual = assertEquals = failUnlessEqual

In Python 3, to your point, failUnlessEqual is explicitly deprecated. assertEquals carries this comment :-)

# Synonyms for assertion methods

# The plurals are undocumented. Keep them that way to discourage use.

# Do not add more. Do not remove.

# Going through a deprecation cycle on these would annoy many people.

So, the upshot appears to be that you should use whatever you like for Python 2.x, but tend toward assertEqual for Python 3.

INFORMATION

결론: assertEqual 써라

Parametrize

WARNING

참고로 pytest의 @pytest.mark.parametrize데코레이터는 unittest.TestCase 클래스를 상속받는 구조에서는 작동하지 않는다.

Plugins

Troubleshooting

HINT: remove pycache / .pyc files and/or use a unique basename for your test file modules

import file mismatch:
imported module 'test_msgpack' has this __file__ attribute:
  /home/your/Project/type-serialize/test/test_msgpack.py
which is not the same as the test file we want to collect:
  /home/your/Project/type-serialize/test/byte/test_msgpack.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

테스트를 돌렸는데 위와 같은 에러가 나왔다면 테스트 디렉토리에 __init__.py 파일이 존재하는지 확인해 보자.

PytestUnhandledThreadExceptionWarning: Exception in thread {...}

PytestUnhandledThreadExceptionWarning: Exception in thread ThreadTestCase

스레드 안에서 에러가 발생될 경우 해당 에러 처리가 안됐다. <- 해라.

See also

Favorite site