Hướng dẫn python unittest print statement - câu lệnh in đơn nhất python

1

New! Save questions or answers and organize your favorite content. Learn more.
Learn more.

I am new to writing python unit tests, Please help me to write test cases for the below function which is having only print statements instead of return statements.

import os
from pwd import getpwuid
from grp import getgrgid
import time

def info(self):
 if self.f:  // if -f  flag pass with file names then it will be trigger
    for i in self.f:
        owner = getpwuid(os.stat(i).st_uid).pw_name
        group = getgrgid(os.stat(i).st_gid).gr_name
        per = oct(os.stat(i).st_mode)[-3:]
        t = os.stat(i).st_mtime
        print("{} is owned by: {} , group by {} with "
              "permission {} and last modified on {}"
              .format(i, owner, group, per, time.ctime(t)))

asked Sep 2, 2021 at 19:40Sep 2, 2021 at 19:40

Hướng dẫn python unittest print statement - câu lệnh in đơn nhất python

3

You can use contextlib's redirect_stdout

import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")

answered Sep 2, 2021 at 19:50Sep 2, 2021 at 19:50

ti7ti7ti7

13.7k6 gold badges32 silver badges60 bronze badges6 gold badges32 silver badges60 bronze badges

2

Kết quả thực hiện: >python -m unittest test_list_value.py.----------------------------------------------------------------------Ran 1 test in 0.000s

Ví dụ 3: Xử lý chuỗi palindromeChuỗi palindrome có dạng: Chuỗi lớn hơn 1, Không phân biệt chữ hoa, thường, viết xuôi hay ngược đều thu được kết quả như nhau.Hãy viết chương trình kiểm tra một chuỗi có phải là chuỗi palindrome không ?

Dữ liệu mẫu:

Input: test_str = ”Civic”Output: True
Unit-test là một thuật ngữ trong kiểm thử phần mềm, được định nghĩa là kiểm thử mức đơn vị. Có nghĩa là đặt các bài test vào các thành phần "đơn vị" (unit) của hệ thống phần mềm.

Input: test_str = ”Noon”Output: True

Input: test_str = ” Python”Output: False

Code của bài toán:

Unitest của bài toán:

Thực hiện test bài test:

> python -m unittest test_palindrome.----------------------------------------------------------------------Ran 1 test in 0.000s

4. Một số yêu cầu với unit-test.

  • Unit-test phải ngắn gọn, dễ hiểu, dễ đọc, có thể sẽ phải có đầy đủ mô tả cho từng nhóm dữ liệu input/output.
  • Mỗi unit-test cần phát triển riêng biệt, không nên thiết kế output của unit-test này là input của unit-test tiếp theo.
  • Khi đặt tên unit-test cần đặt tên gợi nhớ hoặc theo quy chuẩn của từng nhóm phát triển để tường minh việc unit-test này đang test cho unit nào.
  • Mỗi unit-test chỉ nên thực hiện test cho một unit, nếu các unit có về input/output hoặc code thì chấp nhận việc duplicate các unit-test.

5. Lợi ích của unit-test.

  • Khi có unit-test, các lập trình viên có thể tự tin mà refactor code.
  • Khi thực hiện chạy unit-test, đôi khi lập trình viên sẽ phát hiện ra lỗi tiềm ẩn (lỗi truy vấn đến database chẳng hạn).
  • Nếu làm việc cộng tác trong team, việc đặt unit-test như các rule trong tiến trình CI sẽ ngăn được trường hợp sản phẩm bị nhưng vẫn được triển khai trên PRODUCTION.

Cách sử dụng unit test trong Python.

Với mỗi ngôn ngữ lập trình lại có các công cụ, thư viện khác nhau để thực hiện viết unit-test. Trong Python, có thể sử dụng pytest và unittest để viết các unit-test. Do unittest có độ thông dụng nhiều hơn nên bài viết sau đây của tôi sẽ tập trung vào module unittest trong Python.

1. Giới thiệu về unittest.

Trong lập trình thì cách giới thiệu nhanh nhất cho một module/thư viện chính là ... lập trình dựa trên các đặc tính của module/thư viên đó. Sau đây là một ví dụ nhỏ về unitest. Các bạn hãy tạo một file có tên simple_unittest.py và gõ vào đoạn code như dưới đây.

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)

Từ màn hình terminal, thực hiện chạy file trên, chúng ta thu được kết quả:

import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
0
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
1
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
2
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
3
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
4
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
1
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
2
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
3
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
4

import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
5
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
6
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
7
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
8
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
9
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
0
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
1
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
6
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
7
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
8
import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
9
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
0
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
1

import io
import unittest
from contextlib import redirect_stdout

import lib_containing_info

class TestInfo():
    def test_info(self)
        f = io.StringIO()
        with redirect_stdout(f):
            lib_containing_info.info()  # function being tested
        self.assertRegex(f.getvalue(), r"is owned by: ")
7
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
3
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
3

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
4

Bây giờ chúng ta cùng nhau phân tích một chút về ví dụ trên.

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
5 --> Có nghĩa là module unittest là module đã được cài đặt cùng với gói cài đặt của Python.

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
6: --> module unittest cung cấp một class unittest.TestCase để các class khác thực hiện kế thừa.

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
7:, def test_isupper(self):, def test_split(self): --> các function đều bắt đầu bằng test_

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
8 --> Để khởi chạy các test case trong một module, cần đặt gọi đến unittest.main() của module đó. unittest.main() thường đặt ở cuối cùng của module (file code).

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
9 --> Các function dùng để so sánh và raise lên các message thông báo cho kết quả test chính chính xác hay không.

Trong trường kết quả test không chính xác, sẽ hiển thị ra bài test không pass được bằng cách trỏ đến dòng code và nguyên nhân gây ra lỗi.Như ví dụ trên, lỗi nằm ở dòng só 14 khi cố tình đặt

# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
0, và messagelỗi cũng chỉ ra đúng dòng lỗi là dòng 14, và nôi dung lỗi cũng rõ ràng
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
1
Như ví dụ trên, lỗi nằm ở dòng só 14 khi cố tình đặt
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
0, và messagelỗi cũng chỉ ra đúng dòng lỗi là dòng 14, và nôi dung lỗi cũng rõ ràng
# test_simple_unittest.py
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('python'.upper(), 'PYTHON')

    def test_isupper(self):
        self.assertTrue('PYTHON'.isupper())
        self.assertFalse('Python'.isupper())

    def test_islower(self):
        self.assertTrue('PYTHON'.islower())
        self.assertFalse('Python'.islower())

    def test_split(self):
        test_string = 'python is a best language'
        self.assertEqual(test_string.split(),
                        ['python', 'is', 'a', 'best', 'language'])
        # check that test_string.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            test_string.split(2)

if __name__ == '__main__':
    unittest.main(verbosity=2)
1

2. Một số function trong unit-test thường dùng.

2.1. Các function trong unit-test trả về True/False

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
2--> Trả về True: Nếu giá trị value1 == value2--> Trả về False: nếu value1 != value2
--> Trả về True: Nếu giá trị value1 == value2
--> Trả về False: nếu value1 != value2

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
3--> Trả về True: Nếu giá trị value == True--> Trả về False: nếu value1 == False
--> Trả về True: Nếu giá trị value == True
--> Trả về False: nếu value1 == False

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
4--> Trả về True: Nếu giá trị value == False--> Trả về False: nếu value1 == True
--> Trả về True: Nếu giá trị value == False
--> Trả về False: nếu value1 == True


# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
5
# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
6
# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
6

Trả về True: Nếu trong các expressions phát sinh ra lỗi TypeException Trả về False: Nếu trong expressions không phát sinh ra lỗi
Trả về False: Nếu trong expressions không phát sinh ra lỗi

2.2. Các function khác
Method Checks that
assertNotEqual(a, b) a != b
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b)
assertAlmostEqual(a, b) round(a-b, 7) == 0
assertNotAlmostEqual(a, b) round(a-b, 7) != 0
assertGreater(a, b) a > b
assertGreaterEqual(a, b) a >= b
assertLess(a, b) a < b
assertLessEqual(a, b) a
assertRegex(s, r) r.search(s)
assertNotRegex(s, r) not r.search(s)
assertCountEqual(a, b) a and b have the same elements in the same number, regardless of their order

2.3. Các function so sánh các kiểu dữ liệu khác nhau.. Các function so sánh các kiểu dữ liệu khác nhau.

Method Used to compare
assertMultiLineEqual(a, b) strings
assertSequenceEqual(a, b) sequences
assertListEqual(a, b) lists
assertTupleEqual(a, b) tuples
assertSetEqual(a, b) sets or frozensets
assertDictEqual(a, b) dicts

3. Cách chạy unittest.

Ở ví dụ phía trên, ngoài cách gọi trực tiếp vào module/file để thực thi, chúng ta có thể gọi unittest từng đơn vị như sau: file để thực thi, chúng ta có thể gọi unittest từng đơn vị như sau: 

Test cả module::

>

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
7

================================================== ====================

FAIL: test_islower (test_simple_unittest.TestStringMethods)

---------------------------------------------------------- --------------------

Traceback (most recent call last):

& nbsp;"E:\code_learn\Projects\20201206_python-unitest\python-unittest\source_code\test_simple_unittest.py", line 14in test_islower

    self.assertTrue('PYTHON'.islower())self.assertTrue('PYTHON'.islower())

AssertionError: False is not trueFalse is not true

---------------------------------------------------------- --------------------

Ran 4 tests in 0.001s4 tests in 0.001s

FAILED (failures=1)failures=1)

& nbsp;

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
8

================================================== ====================

FAIL: test_islower (test_simple_unittest.TestStringMethods)

---------------------------------------------------------- --------------------

Traceback (most recent call last):

& nbsp;"E:\code_learn\Projects\20201206_python-unitest\python-unittest\source_code\test_simple_unittest.py", line 14in test_islower

    self.assertTrue('PYTHON'.islower())self.assertTrue('PYTHON'.islower())

AssertionError: False is not trueFalse is not true

---------------------------------------------------------- --------------------

Ran 4 tests in 0.001s4 tests in 0.001s

FAILED (failures=1)failures=1)

& nbsp;

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​
9

---------------------------------------------------------- --------------------

Ran 1 test in 0.000s1 test in 0.000s

& nbsp;

# test_first_equation.py

import unittest
import first_equation

class TestFirst(unittest.TestCase):

    def test_find_x(self):
        args = (10, 10)
        self.assertEqual(first_equation.find_x(*args), -1)
        args = (0, 0)
        self.assertEqual(first_equation.find_x(*args), "ALL")
        args = (0, 10)
        self.assertEqual(first_equation.find_x(*args), "NONE")

if __name__ == '__main__':
    unittest.main(verbosity=2)
0

================================================== ====================

FAIL: test_islower (test_simple_unittest.TestStringMethods)

---------------------------------------------------------- --------------------

Traceback (most recent call last):

& nbsp;"E:\code_learn\Projects\20201206_python-unitest\python-unittest\source_code\test_simple_unittest.py", line 14in test_islower

    self.assertTrue('PYTHON'.islower())self.assertTrue('PYTHON'.islower())

AssertionError: False is not trueFalse is not true

---------------------------------------------------------- --------------------

Ran 1 test in 0.000s1 test in 0.000s

FAILED (failures=1)failures=1)

& nbsp;. Một số ví dụ về unittest

Test & nbsp; từng & nbsp; class/function & nbsp; trong & nbsp; mô -đun: Viết chương trình tìm nghiệm của phương trình bậc 1: aX + b = 0

Test & nbsp; từng & nbsp; function & nbsp; trong & nbsp; mô -đun

ĐƯỢC RỒI

4. & nbsp; một & nbsp; số & nbsp; ví & nbsp;0 và b == 0 -> phương trình vô số nghiệm, trả về ALL

Ví & nbsp; dụ & nbsp; 1: & nbs0 và b != 0 -> Phương trình vô nghiệm, trả về NONE

& nbsp; viết & nbsp; đơn vị & nbsp; test & nbsp;

GIảI & NBSP; THUậT:file first_equation.py

# first_equation.py
def find_x(a,b):
    if a:
        return -b/a
    elif b:
        return "NONE"
    else:
        return "ALL"

def find_x_2(a,b):
    return -b/a


if __name__ == "__main__":
    print(find_x(10,10))​

Nếu & nbsp; a & nbsp; == & nbsp; 0 & nbsp; vào

# test_first_equation.py

import unittest
import first_equation

class TestFirst(unittest.TestCase):

    def test_find_x(self):
        args = (10, 10)
        self.assertEqual(first_equation.find_x(*args), -1)
        args = (0, 0)
        self.assertEqual(first_equation.find_x(*args), "ALL")
        args = (0, 10)
        self.assertEqual(first_equation.find_x(*args), "NONE")

if __name__ == '__main__':
    unittest.main(verbosity=2)

Nếu & nbsp; a & nbsp; == & nbsp; 0 & nbsp; vào
> python -m unittest test_first_equation.TestFirst
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

& nbsp;

Test & nbsp; từng & nbsp; class/function & nbsp; trong & nbsp; mô -đun: Cho 1 chuỗi cho trước, thực hiện tách chuỗi theo các khoảng trắng và trả về một list của các tuple dạng [(số thứ tự, giá trị),…]

Test & nbsp; từng & nbsp; function & nbsp; trong & nbsp; mô -đun
test_str = "Python is a best language”

Output:

ĐƯỢC RỒI


4. & nbsp; một & nbsp; số & nbsp; ví & nbsp;

# list_value.py
data = "Python is a best language"

def create_value(data_str):
    input_data = data_str.split()
    result = []
    for index, element in enumerate(input_data):
        result.append((index + 1, element))
    return result

if __name__ == "__main__":
    print(create_value(data))

Ví & nbsp; dụ & nbsp; 1: & nbs

# test_list_value.py
import unittest
import list_value

class TestStringMethods(unittest.TestCase):

    def test_create_tuple(self):
        test_str = list_value.data
        data_list = test_str.split()
        len_expected = len(data_list)
        result_list = list_value.create_value(test_str)
        self.assertIsInstance(result_list, list, "Dữ liệu trả về không đúng dạng list")
        for item in result_list:
            self.assertIsInstance(item, tuple, "Dữ liệu trả về không đúng dạng tuple")
        self.assertEqual(len(result_list), len_expected, "Số lượng phần tử không đúng")
        self.assertEqual(result_list[0][0], 1, 'Index phần tử đầu tiên không đúng')
        self.assertEqual(
            result_list[-1][0],
            len_expected,
            'Index phần tử cuối cùng không đúng'
        )


if __name__ == '__main__':
    unittest.main(verbosity=2)

& nbsp; viết & nbsp; đơn vị & nbsp; test & nbsp;
>python -m unittest test_list_value.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

& nbsp;

Test & nbsp; từng & nbsp; class/function & nbsp; trong & nbsp; mô -đun Xử lý chuỗi palindrome
Chuỗi palindrome có dạng: Chuỗi lớn hơn 1, Không phân biệt chữ hoa, thường, viết xuôi hay ngược đều thu được kết quả như nhau.
Hãy viết chương trình kiểm tra một chuỗi có phải là chuỗi palindrome không ?

Test & nbsp; từng & nbsp; function & nbsp; trong & nbsp; mô -đun

ĐƯỢC RỒI
Output: True

4. & nbsp; một & nbsp; số & nbsp; ví & nbsp;
Output: True


Ví & nbsp; dụ & nbsp; 1: & nbs
Output: False

& nbsp; viết & nbsp; đơn vị & nbsp; test & nbsp;

# palindrome.py

def check_palindrome(text):
    if len(text) <= 1:
        return False
    text = text.strip().lower().replace(' ', '')
    return text == text[::-1]


if __name__ == "__main__":
    text = "Noon"
    print(check_palindrome(text))

GIảI & NBSP; THUậT:

# test_palindrome.py
import unittest
import palindrome


class TestExercise(unittest.TestCase):
    MESSAGE_FMT = 'Kết quả mong muốn là `{0}`, nhận được `{1}`: `{2}`'

    def _test_all(self, func, cases):
        for input_, expect in cases:
            output = func(input_)
            msg = self.MESSAGE_FMT.format(expect, output, input_)
            self.assertEqual(output, expect, msg)


class TestPalindrome(TestExercise):

    def test_check_palindrome(self):
        cases = [('ana', True),
                 ('Civic', True),
                 ('Python', False),
                 ('', False),
                 ('P', False),
                 ('Able was I ere I saw Elba', True)]
        self._test_all(palindrome.check_palindrome, cases)


if __name__ == '__main__':
    unittest.main(verbosity=2)

Nếu & nbsp; a & nbsp; == & nbsp; 0 & nbsp; vào

Nếu & nbsp; a & nbsp; == & nbsp; 0 & nbsp; vào
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

& nbsp;

Test & nbsp; từng & nbsp; class/function & nbsp; trong & nbsp; mô -đun

Test & nbsp; từng & nbsp; function & nbsp; trong & nbsp; mô -đunFAIL --> Đó là lý do tại sao chúng ta cần phải viết unitest. Kết quả fail có nghĩa là chúng ta sẽ phải xem lại phần code chương trình, có thể đã có chỗ nào đó đã bị thay đổi để đưa so với ban đầu khiến cho chương trình hoạt động không được chuẩn xác.

Kết

Trên đây là một số vấn đề cơ bản khi thực hiện viết unittest cho lập trình Python. Hẹn gặp lại các bạn vào một bài viết chuyên sâu hơn, phân tích kỹ hơn các chiến thuật viết unittest và unittest-mock.Source code chương trình được lưu trên github tại link: https://github.com/quangvinh2986/python-unittest.Cảm ơn các bạn đã theo dõi bài viết.
Source code chương trình được lưu trên github tại link: https://github.com/quangvinh2986/python-unittest.
Cảm ơn các bạn đã theo dõi bài viết.

Tài liệu tham khảo: https://docs.python.org/3/library/unittest.html.