Python - How To Write Pytest

Pytest Is The New Black (Compared To Unittest)

Posted by Rico's Nerd Cluster on February 1, 2019

Assert

  • For integer assertions:
1
assert (1==1)
  • For float assertions:
1
2
import pytest
1.0==pytest.approx(1.0)
  • For numpy array assertions:
1
2
3
4
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([1, 2, 3])
np.testing.assert_allclose(array1, array2)

Using VSCode Debugger With Pytest

  1. ctrl+shift+p choose debug tests in the current file or debug all tests (if you want to debug all tests under a configured directory)
  2. In my debugger, I found that I have to manually set a breakpoint before the failure point in Pytest. (I might miss an easier route to get around this)
  3. At the failed test, right click, and choose debug test