Author: admin
-
Python assertEqual
Summary: in this tutorial, you’ll learn how to use the Python assertEqual() method to test if two values are equal. Introduction to the Python assertEqual() method The assertEqual() is a method of the TestCase class of the unittest module. The assertEqual() tests if two values are equal: If the first value does not equal the second value, the test will fail. The msg is optional. If the msg is provided, it’ll…
-
Python Unittest Assert Methods
Summary: in this tutorial, you’ll learn the overview of the Python unittest assert methods to perform unit testing. Introduction to Python unittest assert methods The TestCase class of the unittest module provides you with a large number of assert methods to test. The following table shows the most commonly used assert methods: Method Checks that assertEqual(x, y, msg=None) x == y assertNotEqual(x,y,msg=None) x != y assertTrue(x,…
-
Organizing Code & Running unittest
Summary: in this tutorial, you’ll learn how to organize the test code and how to use the various commands to run unit tests. Organizing code If you have a few modules, you can create test modules and place them within the same directory. In practice, you may have many modules organized into packages. Therefore, it’s important to…
-
Skipping Tests
Summary: in this tutorial, you’ll learn how to skip tests using the Python unittest module. The unittest module allows you to skip a test method or a test class. To skip a test, you have three available options: Skipping a test method examples The following example uses the @unittest.skip() decorator to skip the test_case_2() method unconditionally: Run the test: Output: The output…
-
Python Test Fixtures
Summary: in this tutorial, you’ll learn about Python test fixtures including setUp() and tearDown() methods. Introduction to the Python Test fixtures By definition, a test fixture is a function or method that runs before and after a block of test code executes. In other words, it is a step carried out before or after a test. Module-level fixtures Suppose you have a test…
-
Python unittest
Summary: in this tutorial, you’ll learn about the unit test concept and how to use the Python unittest module to perform unit testing. What is a unit test A unit test is an automated test that: The idea of unit testing is to check each small piece of your program to ensure it works properly. It’s different…
-
Python Regex Cheat Sheet
This page provides a Python regex cheat sheet that you can quickly reference while working with regular expressions. Character sets Pattern Meaning \w Match a single word character a-z, A-Z, 0-9, and underscore (_) \d Match a single digit 0-9 \s Match whitespace including \t, \n, and \r and space character . Match any character except…
-
Python Regex Flags
Summary: in this tutorial, you’ll learn about the Python regex flags and how they change the behavior of the regex engine for pattern matching. Introduction to the Python regex flags The regular expression functions like findall, finditer, search, match, split, sub, … have the parameter (flags) that accepts one or more regex flags. Since Python 3.6, regex flags are instances of…
-
Python Regex split()
Summary: in this tutorial, you’ll learn how to use the Python regex split() function to split a string at the occurrences of matches of a regular expression. Introduction to the Python regex split() function The built-in re module provides you with the split() function that splits a string by the matches of a regular expression. The split() function has the following syntax: In this…
-
Python Regex sub()
Summary: in this tutorial, you’ll learn about the Python regex sub() function that returns a string after replacing the matched pattern in a string with a replacement. Introduction to the Python regex sub function The sub() is a function in the built-in re module that handles regular expressions. The sub() function has the following syntax: In this syntax: The sub() function searches for the pattern in…