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.

Introduction to BDD/TDD - Behaviour Driven Development and Test Driven Development

Behaviour Driven Development started as an attempt to better understand and explain the process of Test Driven Development.

Test Driven Development is a developer practice that involves writing tests before writing the code being tested. Start by writing a very small test for code that does not yet exist. Run the test and it will fail. Then write enough code to make that test pass. That's the idea!

Not all projects have QA or testing teams, and these methodologies and practices help balance the risk and overall quality of the software being built.

As a developer, it helps to think about all the things that could go wrong with any specific code. This is where you start adding edge or boundary cases for the behavior of your code.

TDD arrives to a point in where some confusion with Unit Testing arises, and certain details create a dependency between the internal structure of the object being tested and the test itself. This, as you can imagine, causes a lot of pain to maintain, and is a reason for test suites to become ignored.

BDD tries to come out of this as an improved way to perform "TDD". The problem with TDD is that it tests what the object is and not what it does. What it does is more important.

This evolution on testing methodologies explained that the test should focus on the behavior of what needs to be done, and not the structural-specific details.

With these, frameworks that encourage focusing on behavior instead of internal structure or values inside the object being tested emerged, such as RSpec. To use it, you need to have some basic knowledge of the Ruby programming language.

Monday, April 15, 2013

Basic process handling in Linux - List, Kill processes of specific user

It is very common to encounter basic commands that are very useful, but you forget how to do them or their syntax (maybe you forget the parameters or all the options they have). Specially if you are not constantly using them.

top command displays in console a list of tasks being handled by the kernel.

If you write top -u <username> you can filter those tasks/processes by username.

If you want to kill all processes for a given user, you can use pkill -u <username>.

As always, you can use the help pages while on the command prompt, such as man <command>, or info <command> or even help <command>.

Sunday, April 14, 2013

Start using Selenium Server - Selenium basics

To use Selenium, you need to download the Selenium Server (only a jar file), and the language-specific client drivers that corresponds to the language of your preference (Java, Python, Ruby, etc.). You find all these in the same place: http://docs.seleniumhq.org/download/

Once you perform all the necessary steps for installation (downloading and installing libraries), on your code you will need to import the libraries that will allow you to interact with the selenium server, and perform your automation tasks.

For basic examples on what can be done and how (with actual code and snippets) you can refer to the official Selenium website: Setting up a Selenium WebDriver Project. They have examples in many languages.

With this automation tool, plus some knowledge of general computer science, you can create many useful scripts, even for personal use. If you think 'automation', think also outside of Selenium by programming general software, and add the possibility of those scripts interacting with Selenium itself, or even, using virtual machines to interact and not disturbing your host machine. If you have enough computer power, you can even multiply these to get parallel computing possibilities.

Another useful thing that many do not realize is that, while using certain scripting languages you require the server to be started, that can also be automated very easily from within the code. You can create processes, threads, and make calls to the OS to run Selenium Server and close it at will.

For people working professionally on testing, they probably already know plenty about automation. Selenium adds possibilities to integrate it's findings, and by enabling us the ability to create behavior and logic with our regular programming abilities, we can create reports on many formats and tools enabling further interaction with other software being used by any company working on software development and testing.