Read e-book Python Unit Test Automation : Practical Techniques for Python Developers and Testers

Free download. Book file PDF easily for everyone and every device. You can download and read online Python Unit Test Automation : Practical Techniques for Python Developers and Testers file PDF Book only if you are registered here. And also you can download or read online all Book PDF file that related with Python Unit Test Automation : Practical Techniques for Python Developers and Testers book. Happy reading Python Unit Test Automation : Practical Techniques for Python Developers and Testers Bookeveryone. Download file Free Book PDF Python Unit Test Automation : Practical Techniques for Python Developers and Testers at Complete PDF Library. This Book have some digital formats such us :paperbook, ebook, kindle, epub, fb2 and another formats. Here is The CompletePDF Book Library. It's free to register here to get Book file PDF Python Unit Test Automation : Practical Techniques for Python Developers and Testers Pocket Guide.


  1. Python Unit Test Automation Practical Techniques for Python Developers and Testers 1st ed.
  2. Python Unit Test Automation - Ashwin Pajankar - Häftad () | Bokus
  3. Search Tips

When coming back to work, you will have a pointer to where you were and get back on track faster. The first step when you are debugging your code is to write a new test pinpointing the bug. While it is not always possible to do, those bug catching tests are among the most valuable pieces of code in your project. Use long and descriptive names for testing functions. The style guide here is slightly different than that of running code, where short names are often preferred.

The reason is testing functions are never called explicitly. These function names are displayed when a test fails, and should be as descriptive as possible. When something goes wrong or has to be changed, and if your code has a good set of tests, you or other maintainers will rely largely on the testing suite to fix the problem or modify a given behavior. Therefore the testing code will be read as much as or even more than the running code. A unit test whose purpose is unclear is not very helpful in this case.

Another use of the testing code is as an introduction to new developers. When someone will have to work on the code base, running and reading the related testing code is often the best thing that they can do to start. They will or should discover the hot spots, where most difficulties arise, and the corner cases. If they have to add some functionality, the first step should be to add a test to ensure that the new functionality is not already a working path that has not been plugged into the interface.

TestCase : def test self : self. A simple doctest in a function: def square x : """Return the square of x. TestCase This is one of the ways you can write and execute tests. The test execution did not display much information. Thats because verbosity is disabled by default. You can run the tests in verbose mode using the -v command-line option. Run the following command at the command prompt:. Certainly, the verbose execution mode provides more insight about the test execution. We will be using this mode very frequently throughout the book for running the tests and gathering the log for test executions.

Order of Execution of the Test Methods Now, you will see the order in which the test methods are executed. Check the code in Listing In the code in Listing , inspect. Its useful for debugging when you want to know the order that the methods are executed in the test class. The output of the code in Listing is as follows:. Note that the test methods ran in alphabetical order, irrespective of the order of the test methods in the code. Verbosity Control In earlier examples, you controlled the verbosity of test execution through the command while invoking the Python test script in the OS console.

Now, you will learn how to control the verbose mode from the code itself. See the code in Listing for an example.

Selenium with Python Tutorial 38-Python UnitTest -Creating and Running Test Suites - Batch Testing

In Listing , you are testing a custom function called add with the assertEqual method. If both arguments are equal, the test case passes; otherwise, it fails. We have also written a function called add in the same test module thats not a member of the test class. We are also setting the verbosity to the value 2 in the unittest. Run the code in Listing with the following command:. You now have more information related to the test case failure, since verbosity was enabled in the code.

Now you will see an example Listing of a test module that has multiple test classes. Test Fixtures To put it simply, test fixtures are the set of steps performed before and after the tests. In unittest, these are implemented as methods of the TestCase class and can be overridden for your purposes. An example of custom test fixtures in unittest is shown in Listing ,. In the code in Listing , the setUpModule and tearDownModule methods are the module-level fixtures.

These methods are used with the classmethod decorator, as shown in the code in Listing The classmethod decorator must have a reference to a class object as the first parameter. Run the code in Listing as follows:. In setUpModule In setUpClass In setUp In tearDownModule The test fixtures and their implementation is the key feature in any test automation library. This is a major advantage over the static testing offered by doctest. Running Without unittest. Now you will see how to run the test module without unittest.

Consider the code in Listing , for example. The only way to run this module is to use the Python interpreter with the -m unittest option and the module name, as follows:. Note that you do not need to have. You can also enable verbosity with the -v options, as follows:. We will use this same method throughout the chapter to run test modules.

In later sections of this chapter, you will learn more about this method. For now, run all the previous code examples with this method of execution as an exercise. Controlling the Granularity of Test Execution You learned how to run a test module using the -m unittest option. You can also run individual test classes and test cases using this option. Listing All the Command-Line Options and Help You can list all the command line options of unittest using the -h command-line option. Run the following command:.

TestClass python3 -m unittest module. For test discovery all test modules must be importable from the top level directory of the project. This way you get a detailed summary of the various command-line options available with unittest. Important Command-Line Options Lets take a look at the important command-line options in unittest. Take a look at the code in Listing for example. You already know that -v stands for verbose mode. The following is the output in verbose mode:. The option -q stands for quiet mode. Run the following command to demonstrate quiet mode:. The option -f stands for failsafe.

It forcefully stops execution as soon as the first test case fails. Run the following command to initiate failsafe mode:. You can also use more than one option. For example, you can combine verbose with failsafe using the following command:. Creating a Test Package Up until now, you have created and executed test modules individually.

However, you can use Pythons built-in packaging feature to create a package of tests. This is standard practice in complex projects with large codebases. Figure shows a snapshot of the current test directory where you are saving your test modules. Now, lets create a package of test modules. You just created a test package. Now you can run the test modules from the parent directory of test chapter03 in the following way. Move to the chapter03 directory using the following command:. Note that the path might be different in your case, depending on where you have created the book directory.

Run the test module with the following command:. Organizing the Code Lets look at the ways you can organize the test code and dev. Were now moving toward real-life project scenarios for using unittest. Up until now, the tests the testing code and the code to be tested the development code were in the same module. Usually in the real-life projects, the development code and the test code are kept in different files. Placing the Development and Test Code in a Single Directory Here, you will organize the dev and test code in a single directory.

This way, you can organize the development code and the testing code in the same directory, in the different files. Placing the Development and Test Code in Separate Directories Many coding standards recommend that the development code and the testing code files be organized in separate directories.

Lets do that now. Navigate to the chapter03 directory,. These modules you just created are the development modules.

This will create a Python package for the development code. Now, navigate back to the chapter03 directory. The structure of the chapter03 directory should now look like Figure You now need to create a test module for testing the development code in mypackage. Run the code from the test directory using the following command:. Thats because the mypackage module is not visible from the test directory. It lives not in the test directory, but in the chapter03 directory. This module cannot be executed from the test directory. You must execute this module as a part of the test package.

You can do this from the chapter03 directory. The mypackage module is visible in this directory as mypackage, which is a subdirectory of chapter Navigate to the chapter03 directory and run this module as follows:. Creating object : mymathlib In setUpClass Destroying object : mymathlib. Thats how you organize the development and testing code files in separate directories. It is standard practice to separate these code files.

Python Unit Test Automation Practical Techniques for Python Developers and Testers 1st ed.

Test Discovery Test discovery is the process of discovering and executing all the tests in the project directory and all its subdirectories. The test discovery process is automated in unittest and can be invoked using the discover sub-command. It can be invoked with the following command:. As you can see in the verbose output, the unittest automatically found and ran all the test modules located in the chapter03 directory and its subdirectories.

This saves you the pain of running each test module separately and collecting the results individually. Test discovery is one of the most important features of any automation testing framework. Coding Conventions for unittest As you have seen, test discovery automatically finds and runs all the tests in a project directory.

To achieve this effect, you need to follow some coding and naming conventions for your test code. You may have noticed already that I have consistently followed these conventions in all the code examples in this book. In order to be compatible with test discovery, all of the test files must be either modules or packages importable from the top- level directory of the project. By default, the test discovery always starts from the current directory.

Assertions in unittest You have learned about a few basic assertions, like assertEqual and assertTrue. The following tables list the most used assertions and their purpose. All the assert methods listed in the previous tables are good enough for most of the programmers and testers for automating the tests. Other Useful Methods This section looks at a few useful methods that will help you debug and understand the flow of execution. The id and shortDescription methods are very useful for debugging.

  1. Python Unit Test Automation : Practical Techniques For Python Developers And Testers.
  2. Python Unit Test Automation.
  3. Frost.
  4. Moths, Myths, and Mosquitoes: The Eccentric Life of Harrison G. Dyar, Jr.
  5. The Wall of the Sky, the Wall of the Eye.
  6. Classical Literature: A Concise History (Blackwell Introductions to the Classical World).
  7. Pregnancy and Childbirth: A holistic approach to massage and bodywork?

Listing shows an example. TestClass11 This is a test method Failing a Test Many times, you might want to have a method that explicitly fails a test when its called. In unittest, the fail method is used for that purpose. Check the code in Listing as an example. TestClass12 This is a test method It uses the following decorators for implementing the skipping mechanism: unittest. If the test fails when it runs, the test is not counted as a failure.

The code in Listing demonstrates how to skip tests conditionally and unconditionally. When you run the code in Listing on the Windows platform, the output is as follows:. As you can see, the code skips the test cases based on the OS where it runs. This trick is very useful for running platform-specific test cases.

You can also skip entire test classes in a test module using the unittest.

Python Unit Test Automation - Ashwin Pajankar - Häftad () | Bokus

Exceptions in the Test Case When an exception is raised in a test case, the test case fails. The code shown in Listing will raise an exception explicitly. The failure message shown when the test fails due to an exception is different from when the test fails due to an assertion. The assertRaises method is used to check if the code block raises the exception mentioned in assertRaises. If the code raises the exception then the test passes; otherwise, it fails. The code shown in Listing demonstrates the usage of assertRaises in detail.

In the code in Listing , we defined a class called Calculator that has two different methods for the addition operation. The add1 method does not have a provision to raise an exception if a non-numeric argument is passed to it. The add2 method raises a ValueError if any of the arguments are non-numeric.

Here is the output of the code in Listing So, I recommend you complete the following exercises to gain more knowledge and experience with unittest. Write code to demonstrate their functionality. Hint: Try to run the code in Listing by enabling each commented-out raise Exception line, one line at a time. This will help you understand how an individual fixture behaves when you raise an exception in it. Conclusion In this chapter, you learned about several important concepts, including test fixtures, test classes, test methods, and test modules.

  • Philosophy of Mathematics: Set Theory, Measuring Theories, and Nominalism;
  • Studies in Soviet Thought?
  • International Perspectives on Maps and the Internet?
  • You also learned how to implement all these concepts with unittest. Almost all the concepts you learned in this chapter will be revisited in later chapters that cover other Python testing frameworks. In the next chapter, we will look at nose and nose2, which are two other popular Python test automation frameworks. The last chapter introduced xUnit and unittest. In this chapter, we will explore yet another unit testing API for Python, called nose. The tagline of nose is, nose extends unittest to make testing easier.

    You can use noses API to write and run automated tests. You can also use nose to run tests written in other frameworks like unittest. This chapter will also explore the next actively developed and maintained iteration of nose, nose2. Introduction to nose nose is not the part of Pythons standard library.

    You have to install it in order to use it. Lets see how we can install it on Python 3. Installing nose on Linux OS The easiest way to install nose on a Linux computer is to install it using Pythons package manager pip. Pip stands for pip installs packages. Its a recursive acronym. If pip is not installed on your Linux computer, you can install it by using a system package manager.

    Ashwin Pajankar 65 A. Install nose with the following command:. Verifying the Installation Once nose is installed, run the following command to verify the installation:. Getting Started with nose To get started with nose, follow the same path of exploration that you followed with unittest. Create a directory called chapter04 in the code directory and copy the mypackage directory from the chapter03 directory to code.

    You will need it later. Create a directory called test too. After all this, the chapter04 directory structure should look like the structure shown in Figure If you compare this code with the simplest test case in the unittest framework, you will notice that you do not have to extend the test from any parent class. This makes the test code cleaner and less cluttered.

    If you try to run it with the following commands, it will not yield any output:. This is because you have not included a test-runner in the code. You can run it by using the -m command-line option for Python as follows:. Running the Test Module with nosetests You can use noses nosetests command to run the test modules as follows:.

    Using the nosetests command is the simplest way to run the test modules. Due to the simplicity and convenience of the coding and invocation style, we will use nosetests to run the tests until we introduce and explain nose2. Organizing the test code In the previous chapter, you learned how to organize the development and the testing code of the project in separate directories. You will follow the same standard in this and the next chapter too. First create a test module to test the development code in mypackage.

    Save the code shown in Listing in the test directory. Listing creates a test class called TestClass As discussed earlier, you do not have to extend it from a parent class. The line containing assert checks if the statement mymathlib. The test package is ready now. You can run the tests from the chapter04 directory as follows:. The convention for running a specific test class is a bit different in nose. The following is the example:. Test Discovery You learned about test discovery in an earlier chapter.

    In fact, test discovery in nose is even simpler than in unittest. You do not have to use the discover sub-command for test discovery. You just need to navigate to the project directory chapter04 in this case and run the nosetests command, as follows:. As you can see in the output, nosetests automatically discovers the test package and runs all its test modules. Fixtures for Classes, Modules, and Methods nose provides xUnit-style fixtures that behave in similar way as the fixtures in unittest.

    Even the names of the fixtures are same. Consider the code in Listing In order to get more details of test execution, you need to add the -s option to the command line, which allows any stdout output to be printed in the command line immediately. From now onward, we will add the -s option to the nosetests command while executing the tests. Fixtures for Functions Before you get started with the fixtures for functions, you must understand the difference between a function and a method in Python.

    A function is a named piece of code that performs an operation and a method is a function with an extra parameter thats the object on which it runs. A function is not associated with a class. A method is always associated with a class. They are not associated with a class. Lets run this code with the following command:. Actually, unittest does not support the concept of standalone test functions, as everything has to be extended from the TestCase class and a function cannot be extended.

    You can name that anything you want except, of course, for Python 3s reserved keywords. Fixtures for Packages unittest does not have a provision for package-level fixtures. Package fixtures are executed when the test package or part of the test package is invoked. If you run a module in this package now, the package-level fixtures will run before beginning any test and after the entire test in the package.

    In setUpPackage In tearDown In tearDownClass Destroying object : mymathlib In tearDownPackage Alternate Names of the nose Fixtures This table lists the alternate names of the nose fixtures. FAIL test. Both the test cases failed due to incorrect test inputs. Note the difference between the logs printed by these test methods. Testing Tools nose. This section looks at a few of those testing tools. They also come with a parameter for an error message when the test case fails.

    The code in Listing demonstrates this. The raises Decorator When you use raises decorator before the test, it must raise one of the exceptions mentioned in the list of exceptions associated with the raises decorator. Listing demonstrates this idea. You can cleverly use this to write negative test cases. The timed decorator If you are using a timed decorator with the test, then the test must finish within the time mentioned in the timed decorator to pass.

    The code in Listing demonstrates that idea. This test fails, as it takes more time to finish the execution of the test than is allotted in the timed decorator. The output of execution is as follows:. TimeExpired: Time limit 0.

    It is the collection or group of related tests that can be executed or scheduled to be executed together. Report Generation Lets look at the various ways to generate comprehensible reports using nose. These are xUnit-style formatted reports. You have to use --with-xunit for generating the report. The report is generated in the current working directory.

    Search Tips

    Run the following command in the test directory:. You have to install a plugin for that. Run the following command to install the HTML output plugin:. The plugin saves the output in the current location in a file called nosetests. Figure shows a snapshot of the nosetests. Creating Color Output in the Console Until now, you saw the methods that generate formatted output files.


    While running nosetest, you must have observed that the console output is monochrome white text on a dark background and vice versa. The plugin called rednose is used to create colored console output. You can install that plugin using the following command:. Figure shows a screenshot of the output, although you wont see it in color here, due to the grayscale nature of the published book.

    Running unittest Tests from nose In the beginning of the chapter, you read that you can run unittest tests with nose. Lets try that now. Navigate to the chapter03 directory. Run the following command to discover and execute all of the unittest tests automatically:. I am truncating the output as it would otherwise fill a lot of pages.

    Run the command yourself to see the entire output. Advantages of nose over unittest Here is a summary of the advantages of nose over unittest: Unlike unittest, nose does not require you to extend test cases from a parent class. This results in less code. Using nose, you can write test functions. This is not possible in unittest. In addition to the regular unittest fixtures, nose has package- and function-level fixtures. Test discovery is simpler in nose than in unittest, as nose does not need a Python interpreter with the discover sub-command.

    Disadvantages of nose The only and the biggest disadvantage of nose is that it is not under active development and has been in maintenance mode for the past several years. It will likely cease without a new person or team to take over its maintenance. If youre planning to start a project and are looking for a suitable automation framework for Python 3, then pytest, nose2, or plain unittest. You might be wondering why I even spent time covering nose if it is not being actively developed.

    The reason is that learning a more advanced framework like nose helps you understand the limitations of unittest. Using Nose 2 nose2 is the next generation of testing for Python. It is based on the plugins branch of unittest2. It is easier for users to configure. It simplifies internal interfaces and processes.

    It supports Python 2 and 3 from the same codebase. It encourages greater community involvement in its development. Unlike nose, it is under active development. It can be installed conveniently using the following command:. Once installed, nose2 can be invoked by running nose2 at the command prompt. It can be used to auto-discover and run the unittest and nose test modules. Run the nose2 -h command at the command prompt to get help with the various nose2 command-line options. The following are the important differences between nose and nose2: Python versions nose supports Python version 2.

    Test loading nose loads and executes test modules one by one, which is called lazy loading. On the contrary, nose2 loads all the modules first and then executes them all at once. Test discovery Because of the difference between the test loading techniques, nose2 does not support all the project layouts. The layout shown in Figure is supported by nose. However, it will not be loaded correctly by nose2. Check if the codebase in your organization is using unittest, nose, or nose2. Consult with the owners of the codebase and plan a migration from these frameworks to a better and more flexible unit-testing framework.

    Conclusion In this chapter, you learned about the advanced unit testing framework nose. Unfortunately, it is not being developed actively so you need to use nose2 as a test-runner for nose tests. In the next chapter, you will learn about and explore an advanced test automation framework called py.

    In an earlier chapter, we explored nose, which is an advanced and better framework for Python testing. Unfortunately, nose has not been under active development for the past several years. That makes it an unsuitable candidate for a test framework when you want to choose something for a long-term project. Moreover, there are many projects that use unittest or nose or a combination of both. You definitely need a framework that has more features than unittest, and unlike nose, it should be under active development.

    You need a unit test framework thats capable of discovering and running tests written in unittest and nose. It should be advanced and must be under active development. The answer is pytest. This chapter extensively explores a modern, advanced, and better test automation framework, called pytest. First, youll learn how pytest offers traditional xUnit style fixtures and then you will explore the advanced fixtures offered by pytest. Introduction to pytest pytest is not a part of Pythons standard library. We have to install it in order to use it, just like we installed nose and nose2. Lets see how we can install it for Python 3.

    This installs pytest for Python 3. You can check the installed version by running the following command:. This is pytest version 3. Ashwin Pajankar 87 A. Simple Test Before you begin, create a directory called chapter05 in the code directory. Copy the mypackage directory as it is from the chapter04 directory. Create a directory called test in chapter Save all the code files for this chapter in the test directory. Just like when using nose, writing a simple test is very easy. See the code in Listing as an example.

    In Listing , we are importing pytest in the first line. Recall that assert is a Python built-in keyword. Also, just like with nose, we do not need to extend these tests from any class. This helps keep the code uncluttered. Running Tests with the py. For the sake of simplicity and convenience, from now onward, we will use the same method to run these tests for rest of the chapter and book. We will use pytest in the next chapter to implement test-driven development. Also, observe when you run your own tests that the output of test execution is in color by default, although the book shows the results in black and white.

    You do not have to use any external or third-party plugin for this effect. Figure shows a screenshot of an execution sample. Test Class and Test Package in pytest Like all the previous test automation frameworks, in pytest you can create test classes and test packages. Take a look at the code in Listing as an example. You can see the output by running the previous command. You can also use the following command to run a test package in verbose mode.

    Test Discovery in pytest pytest can discover and automatically run the tests, just like unittest, nose, and nose2 can. Run the following command in the project directory to initiate automated test discovery:. New York: Apress. Ships with Tracking Number! May not contain Access Codes or Supplements. May be ex-library. Buy with confidence, excellent customer service! More tools Find sellers with multiple copies Add to want list.

    Didn't find what you're looking for? Add to want list. Are you a frequent reader or book collector? Social responsibility Did you know that since , Biblio has used its profits to build 12 public libraries in rural villages of South America? A special order item has limited availability and the seller may source this title from another supplier.

    In this event, there may be a slight delay in shipping and possible variation in description.