0x07. python - test-driven development

lazykoala729

New member
#Python #Testing #Riven #development #Software

## Thử nghiệm Python với Riven

Riven là một khung thử nghiệm Python giúp bạn dễ dàng viết và chạy các bài kiểm tra.Nó được thiết kế để có trọng lượng nhẹ và dễ sử dụng, trong khi vẫn cung cấp một bộ tính năng mạnh mẽ.

Riven được xây dựng trên đỉnh của [Unittest] (https://docs.python.org/3/l Library/unittest.html) và cung cấp một số tính năng giúp viết thử nghiệm dễ dàng hơn, bao gồm:

*** Thử nghiệm Discovery: ** Riven có thể tự động khám phá các bài kiểm tra trong dự án của bạn, giúp bạn dễ dàng chạy tất cả các bài kiểm tra của bạn với một lệnh duy nhất.
*** Các bài kiểm tra tham số hóa: ** Riven cho phép bạn viết các bài kiểm tra có thể chạy với các tham số khác nhau, giúp bạn dễ dàng kiểm tra các kịch bản khác nhau.
*** MOCKED: ** RIVEN cung cấp một số công cụ để chế giễu các phụ thuộc, giúp bạn dễ dàng kiểm tra mã của bạn một cách cô lập.

Riven là một khung thử nghiệm mạnh mẽ và dễ sử dụng, có thể giúp bạn viết các bài kiểm tra tốt hơn cho mã Python của bạn.

### Bắt đầu với Riven

Để bắt đầu với Riven, bạn có thể cài đặt nó bằng PIP:

`` `
PIP Cài đặt Riven
`` `

Khi Riven được cài đặt, bạn có thể tạo một tệp kiểm tra mới bằng cách sử dụng lệnh `Riven init`.Điều này sẽ tạo một tệp mới có tên là `test_my_module.py` trong thư mục hiện tại.

Tệp `test_my_module.py` sẽ chứa một trường hợp thử nghiệm cơ bản mà bạn có thể sử dụng để bắt đầu.Bạn có thể chạy trường hợp kiểm tra bằng cách sử dụng lệnh sau:

`` `
python -m riven test_my_module
`` `

Điều này sẽ chạy trường hợp thử nghiệm và in kết quả vào bảng điều khiển.

### Bài kiểm tra viết với Riven

Các bài kiểm tra Riven được viết bằng khung Unittest.Để viết một trường hợp kiểm tra, bạn cần tạo một lớp con của lớp `unittest.testcase`.

Trong trường hợp thử nghiệm của bạn, bạn có thể xác định một số phương pháp kiểm tra.Mỗi phương pháp kiểm tra sẽ bắt đầu với tiền tố `test_`.

Sau đây là một ví dụ về một trường hợp kiểm tra đơn giản:

`` `Python
Nhập khẩu Unittest

từ my_module nhập myClass


lớp mytestcase (unittest.testcase):

def test_my_method (self):
my_class = myClass ()
self.assertequal (my_class.my_method (), 'Xin chào thế giới!')


Nếu __name__ == '__main__':
Unittest.Main ()
`` `

Trường hợp thử nghiệm này xác định một phương thức thử nghiệm duy nhất được gọi là `test_my_method ()`.Phương thức `test_my_method ()` kiểm tra rằng phương thức `myClass.my_method ()` Trả về chuỗi `'Hello World!'`.

### Các bài kiểm tra đang chạy với Riven

Riven cung cấp một số lệnh để chạy thử nghiệm.Sau đây là các lệnh được sử dụng phổ biến nhất:

* `Python -M Riven Test`: Lệnh này chạy tất cả các thử nghiệm trong thư mục hiện tại.
* `Python -M Riven Test <sodule_name>`: Lệnh này chạy tất cả các thử nghiệm trong mô -đun được chỉ định.
* `Python -M Riven Test <sest_case_name>`: Lệnh này chạy trường hợp thử nghiệm được chỉ định.

Bạn cũng có thể sử dụng cờ `-V` để in thêm đầu ra dài dòng khi chạy thử nghiệm.

### Bài kiểm tra gỡ lỗi với Riven

Riven cung cấp một số công cụ để gỡ lỗi các bài kiểm tra.Sau đây là các công cụ được sử dụng phổ biến nhất:

* Các phương thức `unittest.testcase.assert* ()`: Các phương thức này có thể được sử dụng để khẳng định rằng các điều kiện nhất định được đáp ứng.
* `Unittest.testcase.fail ()` Phương thức: Phương pháp này có thể được sử dụng để thất bại trong trường hợp thử nghiệm.
* Phương thức `unittest.testcase.skip ()`: Phương pháp này có thể được sử dụng để bỏ qua trường hợp thử nghiệm.

Bạn cũng có thể sử dụng trình gỡ lỗi Python để gỡ lỗi các bài kiểm tra.

### Phần kết luận

Riven là một khung thử nghiệm mạnh mẽ và dễ sử dụng, có thể giúp bạn viết các bài kiểm tra tốt hơn cho mã Python của bạn.Riven là một lựa chọn tuyệt vời cho các nhà phát triển của tất cả các cấp độ kinh nghiệm.

## hashtags

* #Python
* #Testing
* #Riven
* #phát triển
* #phần mềm
=======================================
#Python #Testing #Riven #development #Software

## Python Testing with Riven

Riven is a Python testing framework that makes it easy to write and run tests. It is designed to be lightweight and easy to use, while still providing a powerful set of features.

Riven is built on top of the [unittest](https://docs.python.org/3/library/unittest.html) framework, and provides a number of features that make it easier to write tests, including:

* **Test discovery:** Riven can automatically discover tests in your project, making it easy to run all of your tests with a single command.
* **Parameterized tests:** Riven allows you to write tests that can be run with different parameters, making it easy to test different scenarios.
* **Mocking:** Riven provides a number of tools for mocking dependencies, making it easy to test your code in isolation.

Riven is a powerful and easy-to-use testing framework that can help you write better tests for your Python code.

### Getting Started with Riven

To get started with Riven, you can install it using pip:

```
pip install riven
```

Once Riven is installed, you can create a new test file by using the ` riven init` command. This will create a new file called `test_my_module.py` in the current directory.

The `test_my_module.py` file will contain a basic test case that you can use to get started. You can run the test case by using the following command:

```
python -m riven test_my_module
```

This will run the test case and print the results to the console.

### Writing Tests with Riven

Riven tests are written using the unittest framework. To write a test case, you need to create a subclass of the `unittest.TestCase` class.

In your test case, you can define a number of test methods. Each test method should start with the `test_` prefix.

The following is an example of a simple test case:

```python
import unittest

from my_module import MyClass


class MyTestCase(unittest.TestCase):

def test_my_method(self):
my_class = MyClass()
self.assertEqual(my_class.my_method(), 'Hello world!')


if __name__ == '__main__':
unittest.main()
```

This test case defines a single test method called `test_my_method()`. The `test_my_method()` method tests that the `MyClass.my_method()` method returns the string `'Hello world!'`.

### Running Tests with Riven

Riven provides a number of commands for running tests. The following are the most commonly used commands:

* `python -m riven test`: This command runs all of the tests in the current directory.
* `python -m riven test <module_name>`: This command runs all of the tests in the specified module.
* `python -m riven test <test_case_name>`: This command runs the specified test case.

You can also use the `-v` flag to print more verbose output when running tests.

### Debugging Tests with Riven

Riven provides a number of tools for debugging tests. The following are the most commonly used tools:

* The `unittest.TestCase.assert*()` methods: These methods can be used to assert that certain conditions are met.
* The `unittest.TestCase.fail()` method: This method can be used to fail a test case.
* The `unittest.TestCase.skip()` method: This method can be used to skip a test case.

You can also use the Python debugger to debug tests.

### Conclusion

Riven is a powerful and easy-to-use testing framework that can help you write better tests for your Python code. Riven is a great choice for developers of all levels of experience.

## Hashtags

* #Python
* #Testing
* #Riven
* #development
* #Software
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock