Kiểm thử phần mềm có yêu cầu python không?

Kiểm thử phần mềm là quá trình trong đó nhà phát triển đảm bảo rằng đầu ra thực tế của phần mềm phù hợp với đầu ra mong muốn bằng cách cung cấp một số đầu vào kiểm tra cho phần mềm. Kiểm thử phần mềm là một bước quan trọng vì nếu được thực hiện đúng cách, nó có thể giúp nhà phát triển tìm ra lỗi trong phần mềm trong khoảng thời gian ngắn hơn. Kiểm thử phần mềm có thể được chia thành hai loại, Kiểm thử thủ công và Kiểm thử tự động. Kiểm thử tự động là việc thực hiện kiểm thử của bạn bằng tập lệnh thay vì con người. Trong bài viết này, chúng ta sẽ thảo luận về một số phương pháp kiểm thử phần mềm tự động với Python. Hãy viết một ứng dụng đơn giản mà chúng tôi sẽ thực hiện tất cả các bài kiểm tra.  

Python3




pip install unittest
8
pip install unittest
9

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
4

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______6

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3_______13_______1
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
3

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
6
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5
pip install nose2
0

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______22_______4
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
1
pip install nose2
7
pip install nose2
7
pip install nose2
9

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
nose2
2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______34_______6

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______22_______4
nose2 -s DIR_ADD DIR_NAME
1
pip install nose2
7
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
1

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
nose2 -s DIR_ADD DIR_NAME
7
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______13_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______53_______1_______53_______2
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME
3

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______53_______7
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME
9
pip install pytest
0
pip install pytest
1
pip install pytest
2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
pip install pytest
4
pip install pytest
0
pip install pytest
6
pip install pytest
0
pip install pytest
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______72_______0
pip install pytest
0
pip install pytest
1_______62_______2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
py.test
5
pip install pytest
0
pip install pytest
6
pip install pytest
0
pip install pytest
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______01
pip install pytest
0
pip install pytest
1
pip install pytest
2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______0_______06

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______22_______4
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME
7

 

 

pip install unittest
10
pip install unittest
11
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
14
pip install unittest
15

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
17

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
21
pip install pytest
2
pip install unittest
23
pip install pytest
2
pip install unittest
25
pip install unittest
26

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
29

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
31
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
33

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
35

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
37
pip install unittest
38

Ghi chú. Để biết thêm thông tin về hàm __repr__[], hãy tham khảo bài viết này. Bây giờ chúng tôi đã sẵn sàng phần mềm của mình, hãy xem cấu trúc thư mục của thư mục dự án của chúng tôi và sau đó, chúng tôi sẽ bắt đầu kiểm tra phần mềm của mình

---Software_Testing
   |--- __init__.py [to initialize the directory as python package]
   |--- app.py [our software]
   |--- tests [folder to keep all test files]
           |--- __init__.py

Mô-đun 'đơn vị nhất'

Một trong những vấn đề chính với kiểm thử thủ công là nó đòi hỏi thời gian và công sức. Trong thử nghiệm thủ công, chúng tôi kiểm tra ứng dụng qua một số đầu vào, nếu nó không thành công, chúng tôi sẽ ghi lại hoặc chúng tôi gỡ lỗi ứng dụng cho đầu vào kiểm tra cụ thể đó, sau đó chúng tôi lặp lại quy trình. Với unittest, tất cả các đầu vào kiểm tra có thể được cung cấp cùng một lúc và sau đó bạn có thể kiểm tra ứng dụng của mình. Cuối cùng, bạn nhận được một báo cáo chi tiết với tất cả các trường hợp kiểm tra không thành công được chỉ định rõ ràng, nếu có. Mô-đun unittest có cả khung thử nghiệm tích hợp và trình chạy thử nghiệm. Khung thử nghiệm là một bộ quy tắc phải được tuân theo khi viết các trường hợp thử nghiệm, trong khi trình chạy thử nghiệm là một công cụ thực hiện các thử nghiệm này với một loạt các cài đặt và thu thập kết quả. Cài đặt. unittest có sẵn tại PyPI và có thể được cài đặt bằng lệnh sau –

pip install unittest

Sử dụng. Chúng tôi viết các bài kiểm tra trong một mô-đun Python [. py]. Để chạy thử nghiệm, chúng tôi chỉ cần thực thi mô-đun thử nghiệm bằng bất kỳ IDE hoặc thiết bị đầu cuối nào. Bây giờ, hãy viết một số bài kiểm tra cho phần mềm nhỏ của chúng tôi đã thảo luận ở trên bằng cách sử dụng mô-đun unittest

  1. Tạo một tệp có tên tests. py trong thư mục có tên “kiểm tra”
  2. trong các bài kiểm tra. py nhập đơn vị nhất
  3. Tạo một lớp có tên là TestClass kế thừa từ lớp unittest. Trường hợp thử nghiệm. Quy tắc 1. Tất cả các bài kiểm tra được viết dưới dạng các phương thức của một lớp, lớp này phải kế thừa từ lớp unittest. Trường hợp thử nghiệm
  4. Tạo một phương pháp kiểm tra như hình dưới đây. Quy tắc 2. Tên của mỗi và mọi phương thức thử nghiệm phải bắt đầu bằng "test" nếu không nó sẽ bị bỏ qua bởi người chạy thử nghiệm.  

Python3




.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
pip install unittest
40
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
44

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
49_______22_______9
pip install unittest
51
pip install unittest
52

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
54

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
56

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______0_______59
nose2 -s DIR_ADD DIR_NAME
1
pip install unittest
61

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______63
pip install unittest
64
pip install unittest
65

  1. Quy tắc 3. Chúng tôi sử dụng các câu lệnh đặc biệt thay vì các câu lệnh khẳng định tích hợp sẵn trong Python. Đối số đầu tiên của assertEqual[] là đầu ra thực tế, đối số thứ hai là đầu ra mong muốn và đối số thứ ba là thông báo lỗi sẽ được hiển thị trong trường hợp hai giá trị khác nhau [kiểm tra không thành công]
  2. Để chạy các bài kiểm tra mà chúng ta vừa xác định, chúng ta cần gọi phương thức unittest. main[], thêm các dòng sau vào “tests. mô-đun py.  

Python3




pip install unittest
10
pip install unittest
11
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
14
pip install unittest
15

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
73

  1. Vì những dòng này, ngay khi bạn chạy tập lệnh “test. py”, hàm unittest. main[] sẽ được gọi và tất cả các bài kiểm tra sẽ được thực hiện

Cuối cùng là “bài kiểm tra. mô-đun py” phải giống với mã được cung cấp bên dưới.  

Python3




pip install unittest
74
pip install unittest
75

pip install unittest
76
pip install unittest
77_______0_______74
pip install unittest
79

 

pip install unittest
8
pip install unittest
81

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
pip install unittest
40
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______13_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______22_______9
pip install unittest
65

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3_______0_______59
nose2 -s DIR_ADD DIR_NAME
1
pip install unittest
61

pip install unittest
98
pip install unittest
63
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
00
pip install unittest
65

 

pip install unittest
10
pip install unittest
11
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
14
pip install unittest
15

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
73

Đã viết các trường hợp thử nghiệm của chúng tôi, bây giờ chúng ta hãy kiểm tra ứng dụng của mình xem có lỗi nào không. Để kiểm tra ứng dụng của bạn, bạn chỉ cần thực thi tệp kiểm tra “tests. py” bằng dấu nhắc lệnh hoặc bất kỳ IDE nào bạn chọn. Đầu ra phải giống như thế này

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Trong dòng đầu tiên, một. [dấu chấm] đại diện cho một thử nghiệm thành công trong khi 'F' sẽ đại diện cho một trường hợp thử nghiệm thất bại. Cuối cùng, thông báo OK cho chúng tôi biết rằng tất cả các bài kiểm tra đã được thông qua thành công. Hãy thêm một vài bài kiểm tra nữa trong “bài kiểm tra. py” và kiểm tra lại ứng dụng của chúng tôi.  

Python3




pip install unittest
74
pip install unittest
75

pip install unittest
76
pip install unittest
77_______0_______74
pip install unittest
79

 

pip install unittest
8
pip install unittest
81

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
pip install unittest
40
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______13_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______22_______9
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3_______0_______59
nose2 -s DIR_ADD DIR_NAME
1
pip install unittest
61

pip install unittest
98
pip install unittest
63
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
00
pip install unittest
65

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
40
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______2_______47
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
48
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3
pip install unittest
59
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
47
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
54
pip install unittest
61

pip install unittest
98
pip install unittest
63
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
58
pip install unittest
65

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
62
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
3_______13_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______2_______69
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3_______2_______73
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
74_______0_______61

pip install unittest
98
pip install unittest
63
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
78
pip install unittest
65

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
82_______2_______3_______13_______8

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______2_______47
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
90
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______2_______3
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
73
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
47_______2_______54
pip install unittest
61

pip install unittest
98
pip install unittest
63
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
00
pip install unittest
65

 

pip install unittest
10
pip install unittest
11
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
14
pip install unittest
15

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
73

.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]

Một vài điều cần lưu ý trong báo cáo thử nghiệm ở trên là -

  • Dòng đầu tiên biểu thị rằng thử nghiệm 1 và thử nghiệm 3 được thực hiện thành công trong khi thử nghiệm 2 và thử nghiệm 4 không thành công
  • Mỗi trường hợp thử nghiệm thất bại được mô tả trong báo cáo, dòng đầu tiên của mô tả chứa tên của trường hợp thử nghiệm không thành công và dòng cuối cùng chứa thông báo lỗi mà chúng tôi đã xác định cho trường hợp thử nghiệm đó
  • Ở cuối báo cáo, bạn có thể thấy số lần kiểm tra không thành công, nếu không có lần kiểm tra nào thất bại, báo cáo sẽ kết thúc bằng OK

Ghi chú. Để có thêm kiến ​​thức, bạn có thể đọc tài liệu đầy đủ của unittest

Mô-đun “mũi2”

Mục đích của nose2 là mở rộng unittest để giúp việc kiểm tra dễ dàng hơn. mũi2 tương thích với các bài kiểm tra được viết bằng khung kiểm tra unittest và có thể được sử dụng để thay thế trình chạy thử nghiệm unittest. Cài đặt. mũi2 có thể được cài đặt từ PyPI bằng lệnh,

pip install nose2

Sử dụng. mũi2 không có bất kỳ khung thử nghiệm nào và chỉ đơn thuần là một trình chạy thử nghiệm tương thích với khung thử nghiệm kém nhất. Do đó, chúng tôi sẽ chạy các bài kiểm tra tương tự mà chúng tôi đã viết ở trên [đối với bài kiểm tra kém nhất] bằng cách sử dụng mũi2. Để chạy thử nghiệm, chúng tôi sử dụng lệnh sau trong thư mục nguồn dự án [“Software_Testing” trong trường hợp của chúng tôi],

nose2

Trong thuật ngữ mũi2, tất cả các mô-đun python [. py] với tên bắt đầu từ “test” [i. e. test_file. py, kiểm tra_1. py] được coi là tệp thử nghiệm. Khi thực thi, nose2 sẽ tìm kiếm tất cả các tệp kiểm tra trong tất cả các thư mục con thuộc một hoặc nhiều danh mục sau,

  • đó là các gói python [chứa “__init__. py”]
  • có tên bắt đầu bằng “test” sau khi được viết thường, tôi. e. TestFiles, bài kiểm tra
  • được đặt tên là “src” hoặc “lib”

đầu tiên, mũi2 tải tất cả các tệp thử nghiệm có trong dự án và sau đó các thử nghiệm được thực hiện. Do đó, với nose2, chúng tôi có quyền tự do phân chia các bài kiểm tra của mình giữa các tệp kiểm tra khác nhau trong các thư mục khác nhau và thực hiện chúng cùng một lúc, điều này rất hữu ích khi xử lý số lượng lớn các bài kiểm tra. Bây giờ chúng ta hãy tìm hiểu về các tùy chọn tùy chỉnh khác nhau được cung cấp bởi nose2 có thể giúp chúng tôi trong quá trình thử nghiệm

  1. Thay đổi thư mục tìm kiếm – Nếu chúng ta muốn thay đổi thư mục mà nose2 tìm kiếm các tệp thử nghiệm, chúng ta có thể thực hiện việc đó bằng cách sử dụng các đối số dòng lệnh -s hoặc –start-dir như,
nose2 -s DIR_ADD DIR_NAME
  1. ở đây, DIR_NAME là thư mục mà chúng tôi muốn tìm kiếm các tệp thử nghiệm và, DIR_ADD là địa chỉ của thư mục mẹ của DIR_NAME so với thư mục nguồn dự án [i. e. sử dụng ". /” nếu thư mục kiểm tra nằm trong chính thư mục nguồn của dự án]. Điều này cực kỳ hữu ích khi bạn chỉ muốn kiểm tra một tính năng của ứng dụng tại một thời điểm
  2. Chạy các trường hợp thử nghiệm cụ thể – Sử dụng nose2, chúng tôi cũng có thể chạy một thử nghiệm cụ thể tại một thời điểm bằng cách sử dụng các đối số dòng lệnh -s và –start-dir như,
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME
  • TEST_NAME. tên của phương pháp kiểm tra
  • TEST_CLASS. lớp trong đó phương pháp kiểm tra được định nghĩa
  • TEST_FILE. tên của tệp thử nghiệm trong đó trường hợp thử nghiệm được xác định i. e. kiểm tra. py
  • DIR_NAME. thư mục chứa tệp kiểm tra
  • DIR_ADD. địa chỉ của thư mục mẹ của DIR_NAME so với nguồn dự án
  1. Chạy thử nghiệm trong một mô-đun duy nhất – mũi2 cũng có thể được sử dụng như unittest bằng cách gọi hàm mũi2. main[] giống như chúng ta đã gọi là unittest. main[] trong các ví dụ trước

Mô-đun “pytest”

pytest là khung thử nghiệm phổ biến nhất cho python. Sử dụng pytest, bạn có thể kiểm tra mọi thứ từ tập lệnh python cơ bản đến cơ sở dữ liệu, API và giao diện người dùng. Mặc dù pytest chủ yếu được sử dụng để kiểm tra API, nhưng trong bài viết này, chúng tôi sẽ chỉ đề cập đến những điều cơ bản về pytest. Cài đặt. Bạn có thể cài đặt pytest từ PyPI bằng lệnh,

pip install pytest

Sử dụng. Người chạy thử nghiệm pytest được gọi bằng cách sử dụng lệnh sau trong nguồn dự án,

py.test

Không giống như nose2, pytest tìm kiếm các tệp thử nghiệm ở tất cả các vị trí bên trong thư mục dự án. Bất kỳ tệp nào có tên bắt đầu bằng “test_” hoặc kết thúc bằng “_test” đều được coi là tệp thử nghiệm theo thuật ngữ pytest. Hãy tạo một tệp “test_file1. py” trong thư mục “tests” làm tệp thử nghiệm của chúng tôi. Tạo phương pháp thử nghiệm. pytest hỗ trợ các phương thức kiểm tra được viết trong khung unittest, nhưng khung pytest cung cấp cú pháp dễ dàng hơn để viết bài kiểm tra. Xem mã bên dưới để hiểu cú pháp phương thức thử nghiệm của khung pytest.  

Python3




pip install unittest
76
pip install unittest
77_______0_______74
pip install unittest
79

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
15

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______22_______9
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
23
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
24
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2_______13_______2
nose2 -s DIR_ADD DIR_NAME
1
pip install unittest
61

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______13_______30
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
31
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
32
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
33
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
34

 

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
1
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
36

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
pip install unittest
47
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
pip install unittest
90_______2_______47
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
54
pip install unittest
65

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
0
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
23
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
46
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
47
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
54
pip install unittest
61

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
5_______0_______63
.F.F
======================================================================
FAIL: test_area_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 11, in test_area_negative
    self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length']
AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length

======================================================================
FAIL: test_perimeter_negative [__main__.TestSum]
----------------------------------------------------------------------
Traceback [most recent call last]:
  File "tests_unittest.py", line 19, in test_perimeter_negative
    self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length']
AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED [failures=2]
54

Ghi chú. tương tự như unittest, pytest yêu cầu tất cả các tên test bắt đầu bằng “test”. Không giống như unittest, pytest sử dụng các câu lệnh xác nhận python mặc định giúp sử dụng dễ dàng hơn. Lưu ý rằng, bây giờ thư mục “kiểm tra” chứa hai tệp có tên là “kiểm tra. py” [viết bằng unittest framework] và “test_file1. py” [được viết bằng khung pytest]. Bây giờ hãy chạy chương trình thử nghiệm pytest

py.test

Bạn sẽ nhận được một báo cáo tương tự như thu được bằng cách sử dụng unittest

pip install unittest
1

Phần trăm ở phía bên phải của báo cáo cho biết phần trăm bài kiểm tra đã được hoàn thành tại thời điểm đó, tôi. e. 2 trong số 6 trường hợp thử nghiệm đã được hoàn thành khi kết thúc “test_file1. py”. Dưới đây là một vài tùy chỉnh cơ bản đi kèm với pytest

Python có cần thiết để kiểm thử phần mềm không?

Do người thử nghiệm cần một ngôn ngữ lập trình dễ học, phù hợp với nhiều loại tác vụ, được hỗ trợ bởi Selenium WebDriver và thuận tiện cho việc tạo tập lệnh, nên Python là một ngôn ngữ tuyệt vời . .

Ngôn ngữ nào được yêu cầu để kiểm thử phần mềm?

Java là ngôn ngữ lập trình phổ biến nhất được sử dụng để tự động hóa thử nghiệm.

Người kiểm thử phần mềm có yêu cầu lập trình không?

Kết luận. Người kiểm thử không được yêu cầu viết mã giỏi như nhà phát triển, tuy nhiên, học được điều gì đó mới luôn là điều tốt. Nói chung, người kiểm tra thủ công không cần phải học và viết mã .

Python có tốt cho QA không?

Python rất dễ học . Một kỹ sư QA phải tập trung vào các dịch vụ kiểm thử phần mềm và việc học những điều mới không nên trở thành một trở ngại. Cú pháp đơn giản khiến Python trở thành ngôn ngữ lập trình tốt nhất để học từ đầu. Bên cạnh đó, bạn có thể tìm thấy rất nhiều tài liệu hữu ích trên web.

Chủ Đề