Testing in Python Part II: PyTest

After a shallow dive into the world of unit testing in the Python I took to the internet comments for guidance. I know what you’re thinking, it’s an exercise in poor thinking, keyboard warrior syndrome, and people telling me to go fuck myself. However every once in awhile you come across someone who knows what they are talking about and you can get some pretty cool information from them. In this exercise someone suggested to me (on top of definitely upgrading to Python 3) to switch to using Pytest to write my test scripts.

I was, as always, skeptical. I always imagined the person commenting on someone’s post on Google+ was usually looking to start a fight before bed; Using some form of condescension to knock me down a few pegs. This one however was pretty helpful. I started looking into it and he was right. PyTest is actually really cool tool to test your scripts in Python. Not only does it provide a myriad of testing tools, but it’s also very simple.

Installation is easy, I use pip for everything so I simply ran:

pip install -U pytest

Then like some form of magic I have PyTest installed and ready to go.

I wrote some simple tests, and figured out I liked PyTest way more than Unittest. So this will be a breakdown, of the simple scripts that I wrote. Following is the code I wrote, then I’ll explain it. So we first create a file called test4.py and populate it with the code below.

class CreateUser:
     def __init__(self, first_name, last_name):
          self.first_name = first_name
          self.last_name = last_name

user1 = CreateUser('George', 'Offley')

p = "-"
phone = ("866", "740", "4531")
phone_number = p.join( phone )

def test_user():
     assert user1.first_name == "George1" and user1.last_name == "Offley", "This failed because the names don't match"

def test_phone():
    assert phone_number == "866-740-4531"

OK so after we type everything out we can break it down as such. We create a CreatUser class and give it some properties. Then we just set some variables so we can enter a phone number when our user is created. I want the phone number to appear in the correct format when I pass it into the database so use the join() function to make sure it appears as I’d like. Now to the meat.

Much like unittest we create our tests by creating definitions and naming them with a leading test_. We then use the assert method to set up how we want the test to run. in this case I call the instance of our user class called user1:

def test_user():
     assert user1.first_name == "George1" and user1.last_name == "Offley", "This failed because the names don't match"

Now we want to make sure that the user input is equal to the strings we put in. Now we just run:

python -m pytest test4.py

We then get our failure message that we defined into the test. Obviously there’s no 1 in my own name. That’s one cool thing that I like about PyTest, how easy it is to insert your own test failure messages. You just append them on the back of said test.

The second test is a little more straightforward. We only have one equals statement and we want the phone number to appear in the correct format.

def test_phone():
    assert phone_number == "508-587-5294"

We run the command in the terminal again and we get a passing message for this test.

Now PyTest offers a lot more features than just regular testing. It let’s you skip tests, create your own exceptions, and a ton of other things. A lot of those are a little more advanced for me than I’m willing to admit. I’m still learning so I’ll come back with a more comprehensive tutorial in the future. Until then check out the PyTest website for all the cool stuff to help test your code.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply