Python Unit Test - Basic Information and Assertions
Unit Test Example from code.visualstudio.com

Python Unit Test - Basic Information and Assertions


Here, I am going to talk about doing a basic unit test for Python. Now, a unit is a small piece of code in a larger script. A unit could be a method, function, module, class, or small collection of related classes. An automated unit test is a programming test that is designed by a human programmer which runs without any intervention and returns a pass or fail condition based on what results are produced by a unit you are testing. A unite test should not require usage of a filesystem, database, or network.

A unit test will probably include assertions: methods to validate an output the testing it with a known 'correct' response. Here are some commonly used assertions:

 

Assertion Similar To .assertEqual(a, b) a == b .assertTrue(x) bool(x) is True .assertFalse(x) bool(x) is False .assertIs(a, b) a is b .assertIsNone(x) x is None .assertIn(a, b) a in b .assertIsInstance(a, b) isinstance(a, b)

.assertIs().assertIsNone().assertIn(), and .assertIsInstance()  all have opposite methods that can be activated by adding the keyword not: .assertIsNot(), .assertIsNotNone(), etc.

 



Data Science Logs and Scientific Studies
Data Science Logs and Scientific Studies

This blog is about Data Science work and interacting with information as well as code.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.