How to write parametrized tests in Python with pytest 🎥

  • less than 1 minute read
  • 1 code sample

Also available in Română

Hi 👋

Welcome to another video tutorial on how to write parametrized tests in Python using pytest.

https://www.youtube.com/watch?v=o2LLi_iiz-k

Tip

move_zeroes changes the input list in place. Give each parametrized test case a fresh list so mutations from one invocation cannot affect another.

If you want to follow along, here’s the code that I’ve tested in the video.

from typing import Listclass Solution:    def move_zeroes(self, nums: List[int]) -> None:        last_zero = 0        index = 0        while index < len(nums):            if nums[index] != 0:                nums[last_zero], nums[index] = nums[index], nums[last_zero]                last_zero += 1            index += 1def main():    solution = Solution()    arr = [1,0,1]    solution.move_zeroes(arr)    print(arr)if __name__ == '__main__':    main()

Thanks for watching! 😄