在Ubuntu上进行Python单元测试,你可以使用Python的内置模块unittest或者第三方库如pytest
python --version
如果没有安装Python,请使用以下命令安装:
sudo apt update
sudo apt install python3
pytest(可选)。虽然Python自带了unittest模块,但pytest提供了更简洁的语法和更多的功能。要安装pytest,请在终端中输入以下命令:pip3 install pytest
test_my_module.py,并编写你的测试用例。这里是一个简单的例子:import unittest
def add(a, b):
    return a + b
class TestAddFunction(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(add(1, 2), 3)
        self.assertEqual(add(-1, 1), 0)
        self.assertEqual(add(0, 0), 0)
if __name__ == '__main__':
    unittest.main()
使用unittest模块:
python3 -m unittest test_my_module.py
使用pytest:
pytest test_my_module.py
这就是在Ubuntu上进行Python单元测试的基本步骤。你可以根据需要编写更多的测试用例,并使用unittest或pytest提供的功能来组织和运行测试。