Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Pytest of a series of learning triggered by an exercise (1)

Shulou Source: shulou.com Published: 2022-06-03 05:55:47 10月01日 Update

Boxing does not leave the hand does not leave the mouth, daily practice is indispensable!

Today's exercise topic: enter a certain day of a certain month of a certain year, and judge which day of the year it is?

After the code has been written, the work of self-testing is indispensable. I want to try to use tools or frameworks to complete this work.

Code: https://github.com/wanglanqing/Python_Project/tree/master/dayByDay/day4

I. installation

It is very convenient to install using the pip tool, just execute pip install pytest.

Second, write test cases

1. Use case rule

Test files that begin with test_ or end with _ test

Test classes that start with Test

Test methods that begin with test_

There can be no _ _ init__ method in the test class

two。 Normal assertion

Pytest's assertions use assert, which greatly reduces the cost of learning assertions compared to the unittest framework.

Def test_20171231_365 (self): self.d4.get_date (2017, 12, 31) days= self.d4.get_days () assert days==365

two。 Abnormal assertion

For invalid data, exception handling is carried out, and when you first simply use assert, it is found that there will always be errors when executing the case. By using with pytest.raises (Exception) as err_info, you can ExceptionInfo () object and make exception assertions through object's type, match (), value, and so on.

Def test_day_is_minus (self): with pytest.raises (LowThanZero) as err_info: self.d4.get_date (2010) self.d4.get_days () assert err_info.match ('enter a value less than 0')

The use of with pytest.raise () is described in the API provided by python.

> value = 15 > with raises (ValueError) as exc_info:... If value > 10 Vol. Raise ValueError ("value must be"

Tags: Tests beginnings methods code tools reports frameworks work inputs inputting usage parameters commands done total cost data files methods rules Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS Shulou Technology MySQL Shulou Information