Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report