Thursday, May 26, 2016

A little more about Pytest-HTML

As I said before, my project for this summer is about creating enhancements to a plugin named Pytest-HTML, but I didn't say here what is that, in this post, I'll explain a bit.

If you don't know even what is Pytest, I'll try to explain in a few words (if you know, you may skip the following two paragraphs): Pytest is a tool to guarantee that the code you have done in Python is doing what it is supposed to do. Just giving a very simple example, if you have a function named "sum(x1, x2)", what would expect that this function does? Most probably, sum x1 and x2, right? Imagine the situation where x1 and x2 are 2, your expectation is that the function returns 4 to you, right? If it returns any other value (e.g, 9, 8, 2) most probably your function is not doing what it is supposed to do. So to code what I just mentioned above, you would do something like:

 def sum(x1, x2):  
   return x1 + x2 
 def test_sum():  
   assert sum(2, 2) == 4  

So, the function sum is returning x1 + x2, and the test_sum() is asserting that its return is 4. If you copy and paste this code into a file named "" and after installing python and pytest (To install it, run "sudo apt-get install python", and "sudo pip install pytest", in Linux and Mac), you can run on your terminal something like "py.test", it will return the name of your file followed by a single dot indicating that your test has passed, something like " ." . So, it is a very, very quick introduction to Pytest, there is much more you may read about in Python and Pytest websites linked above.

Okay, now you already know what is Pytest, but what is Pytest-HTML? As I said, to indicate that a tests has passed or not, Pytest returns to you a single spot on your terminal. But what if you want the results of your test on a browser? Well, that is what this plugin of Python does: it creates a HTML page (HTML is the language that your browser understands), so instead of seeing just a dot, you see something like the image bellow:

To have your tests in a HTML page like the one above, first you must install (after having python and pytest installed) pytest-html by typing on your terminal "sudo  pip install pytest-html" and after that, run "py.test --html=name_of_html_file.html", and then open the file generated on your browser (by default, the file is generated on the same directory of your test).

If you want to contribute with the development of python, pytest or pytest HTML, you may visit the following gitHub repositories:

No comments:

Post a Comment