![]() Since we’ve verified that we have not handled such a case, we can go ahead and fix it. Running the tests at this point should fail with the following error: def capital_case(x):Į AttributeError: 'int' object has no attribute 'capitalize' The major addition here is the pytest.raises helper, which asserts that our function should raise a TypeError in case the argument passed is not a string. Let’s try to capture this in our test: # test_capitalize.pyĭef test_raises_exception_on_non_string_arguments(): We would like to handle this case in our function by raising a custom exception with a friendly error message to the user. Therefore, if we passed in a number as the argument to the function, it would raise an exception. It does not check the type of the argument to ensure that it is a string. We should see that our first test passes.Ī keen reader will notice that our function could lead to a bug. To run the test, execute the pytest command: $ pytest The immediately noticeable thing is that pytest uses a plain assert statement, which is much easier to remember and use compared to the numerous assertSomething functions found in unittest. # test_capitalize.pyĪssert capital_case('semaphore') = 'Semaphore' We’ll prefix our test function names with test_, since this is what pytest expects our test functions to be named. We will also write a test, test_capital_case to ensure that the function does what it says. Let’s create a file called test_capitalize.py, and inside it we will write a function called capital_case which should take a string as its argument and should return a capitalized version of the string. Pytest expects our tests to be located in files whose names begin with test_ or end with _test.py. To get started, let’s install pytest in our virtualenv. To begin using the virtualenv, we need to activate it as follows: $ source pytest-env/bin/activateĪs long as the virtualenv is active, any packages we install will be installed in our virtual environment, rather than in the global Python installation. This creates a virtual environment called pytest-env in our working directory. To create and activate a virtual environment for this project, let’s run the following commands: $ mkdir pytest_project This tutorial uses Python 3, and we will be working inside a virtualenv.įortunately for us, Python 3 has inbuilt support for creating virtual environments. This tutorial will demonstrate how to write tests for Python code with pytest, and how to utilize it to cater for a wide range of testing scenarios. Pytest stands out among them due to its ease of use and its ability to handle increasingly complex testing needs. In the larger Python ecosystem, there are a lot of testing tools. The Python community embraces testing, and even the Python standard library has good inbuilt tools to support testing. Testing applications has become a standard skill set required for any competent developer today.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |