Get startedGet started for free

Feature testing with pytest

1. Feature testing with pytest

Let's dive into feature testing.

2. What is a feature test

A feature is a software system functionality that satisfies a particular user's requirement. Commonly, a feature is a part of software that is wider than a unit. But sometimes, we can implement a feature as a unit, so they would completely coincide. Hence, feature testing is a testing method that allows to verify the behavior of a specific feature. For example, data distribution check and report preparation are features.

3. Units vs. features: personal computer

But how to differentiate a feature from a unit? The line between units and features is how our software is used. We can ask ourselves: "What the user wants from this particular system?". Let's analyze the example. Suppose we have a personal computer - that is our system. If a unit is an individual keyboard button, then the whole keyboard is the feature. Users don't need individual buttons, but they need the whole keyboard. If a unit is a pixel from the screen, then the screen is a feature. The same with illumination: LED diodes are units, and the illumination is a feature. Finally, the blocks of our hard drive are units, but the file system is a feature. Thus, if the unit is something small that we can test, then the features are that we can actually use.

4. Why do we use feature tests

We use feature tests because they allow us to test software in the scope of a feature. Let's reconsider the keyboard example: if one button does not work, it doesn't mean that one can't use the keyboard. For example, the software might have issues with decoding certain languages. If that is the case, the "keyboard" feature doesn't work, although each individual button does. The important part is that a button does not know anything about the "keyboard" feature as a whole. The opposite is also true: all the buttons work properly, but that does not mean that the keyboard does. If one button is out of order, it does not mean that the feature does not work for users. Perhaps, that was a button that the users do not need, or that will not ruin the experience.

5. Feature test example: setup

Look at this code example. Here, we are filtering the dataset rows with pandas. That is the feature. To test it, we want to make sure that we selected the rows properly. What test cases should we create? For example, we can verify the number of unique values in the "Manufacturer" column equals one. We can also check that the "Manufacturer" column contains a single value. That is exactly the one by which we filtered the dataset.

6. Feature test example: testing

Here is the implementation for the proposed test cases. Note that we did not check the correctness of the function "filter_data_by_manuf" and the functions that it depends on. Instead, we checked that the feature returns a filtered dataset. In a real-life project, we would expand our test suite to cover more test cases to ensure the filtering feature works as expected. Thus, the key to designing feature tests is understanding the features in a specific system example.

7. Summary

We learned about feature testing. Feature testing is a software testing method that allows one to verify the behavior of a specific feature. Features are wider than units and help to ensure that users get what they expect. To create feature tests, one has to create test cases and implement them like unit tests. The key to designing feature tests is understanding the features in a specific system.

8. Let's practice!

Try to create some feature tests of your own!