In this lesson, you will learn:

  • The basics of building a Pytest test.
  • About Python Assert.
  • How to build a basic test in Pytest.
  • How to run a test in Pytest.
  • About Pytests test discovery.

You can find the scripts and code for this lesson within the Pytest repo under the directory: 002_getting_started/001_creating_first_test.

Creating Your First Test

Tests within Pytest are based upon using the assert keyword.

What’s Assert?

Python assert is a built-in keyword within Python that allows us to:

test if a condition in our code returns True.

If the condition after the assert does not return True, then an AssertionError exception will be raised (example shown below):

>>> assert 1 == 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

>>> assert 1 == 1
>>>
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.