InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Selenium tutorials

  • How to test a site using pytest
  • Python selenium webdriver - quick start
  • Pytest - using selenium with PhantomJS
  • Python selenium - execute javascript code
  • Python selenium - print browser log
  • Selenium Phantomjs - check browser errors using pytest
 
  • Home
  • > Tutorials
  • > CI
  • > Selenium

How to test a site using pytest

By admin on Nov 7, 2015

Python is fairly good programming language for writing and maintaining unit and functional tests. One advantage of python is that it does not require compilation like Java and hence debug cycle can be little faster for simple tests. This article will cover pytest (py.test) to test various pages on a website. Here are quick steps to get started with pytest for writing functional tests on Ubuntu Linux.

Install py.test

First install pip (if not installed) and pytest using the following commands:

$ sudo apt-get install python-pip
$ sudo pip install pytest

Write site testing code using pytest

Write the following code in a file test_hello.py in a new directory. Note that file name begins with test_ here as py.test looks for these files when no argument is specified.

#!/usr/bin/python
import pytest, urllib2

class TestHello():
  def test_page(self):
    print "Starting test..."
    url = "https://google.co.in/"
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    data = response.read()
    response.close()
    assert(response.getcode(), 200, "HTTP status code must be 200")
    assert(data.find("</html>") > 0, "response html must have closing html tag")
    print "Done with the test..."

Now run the test using the following command in same directory:

$ py.test 
==================================================================== test session starts ====================================================================
platform linux2 -- Python 2.7.6, pytest-2.8.2, py-1.4.30, pluggy-0.3.1
rootdir: /home/gopj/svn/cloud/infoheap/ui/tests, inifile: 
collected 1 items 

test_hello.py .

================================================================= 1 passed in 0.12 seconds ==================================================================

Note that we checked if the status code of http request is 200 and also the page content should have “” present in it. You can add more checks using similar approach.

Print stdout to screen when using py.test

By default py.test will capture stdout and hence and print statement would not display anything on screen (see the output above). To display output of print statements on screen, use -s option.

$ py.test -s
==================================================================== test session starts ====================================================================
platform linux2 -- Python 2.7.6, pytest-2.8.2, py-1.4.30, pluggy-0.3.1
rootdir: /home/gopj/svn/cloud/infoheap/ui/tests, inifile: 
collected 1 items 

test_hello.py Starting test...
Done with the test...
.

================================================================= 1 passed in 0.11 seconds ==================================================================

Note the presence of “Starting test…” and “Done with the test…” in test outcome on screen this time.

Run a specific test (one method)

$ py.test -s test_hello.py::TestHello::test_page

Suggested posts:

  1. Gmail – how to send email from different address
  2. Python filter list/iterable examples
  3. How to auto forward gmail from one account to another
  4. Python selenium – print browser log (including javascript errors) with phantomjs
  5. How to create and use color palettes in Inkscape
  6. Python re search vs match
  7. Linux du – find disk usage of directories or files
  8. Bash – variables in double quotes vs without quotes
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged pytest, Python, QA, Selenium, Test Automation, Testing, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress