Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, May 18, 2014

Pytest - Python Testing Tool - Testing Framework for Python

Python is a great language that allows you to accomplish things efficiently and effectively in little time. There are many frameworks out there for various purposes and objectives, and in a recent project I have used yet another one for testing, whose flexibility from the command line and extensibility possibilities merited a mention here.

Wednesday, April 24, 2013

Introduction - Basic testing in Python - Using doctest for function tests

You can use doctest to assert that your functions are returning or producing the output you expected.

Here is a very simple function that just returns (as a float type) the multiplication of 2 integers:

def multiply(a,b):
    """ (int,int) -> float

    >>> multiply(2,3)
    6.0
    >>> multiply(3,4)
    12.0
    """
    return float(a*b)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

The important thing to keep from this basic example is that the function returns the multiplication of 2 ints, and converts its result to a float type. The documentation of the function (enclosed by the three ") explains that the function receives 2 ints and returns a float.

Doctest uses the docstring to test the function.

In this case, the call ">>> multiply(2,3)" runs internally in the Python interpreter, and compares the result with the one we wrote. Then it does the same with the second test. If you pay attention you will see that it is as if we were writing those calls in the console ourselves, but in this case using them from the function documentation (Python calls it docstring).

To put it simply, the body of the "if" function is executed if our module, the one with the "multiply" function, is executed directly, and NOT imported. If it is imported, you will be able to use the "multiply" function, but no tests will be run, nor any code in the body of the if statement.

Finally, all these tests run because we used the "import doctest" and "doctest.testmod()" to run these tests. doctest is the module that has the behavior to execute these test cases.

It is very simple, only to serve as a reference to anyone new to the language. Python is being actively used in the scientific community, so a lot of professionals without actual programming experience are using Python. To them, I will aim some of my introductory posts.

Thursday, March 28, 2013

Python testing tools

Here you will find a lot of Python specific tools for testing different technologies and environments.


You can learn and read more about Python on its main website, http://www.python.org/.

Quick solution to share files or create server instance in Python

There is a simple way to share files via web (by creating a Python server instance) by entering a simple python command:

python -m SimpleHTTPServer 9914

You should run the above command on the directory you want to share.

With this command, the directory where we ran this will be shared (including any sub-directories) in the specified port 9914.

To access the files and folders shared, you only need to open a web browser and navigate to our device's IP address (where this command was executed), specifying the port we chose. In this example, "http://192.168.1.1:9914".