Time-tested application testing in Python

I wrote an auction system in Django. I want to write unit tests, but the application is time-sensitive (for example, the number of advertisers charged is dependent on how long their ad has been active on the website). What is a good approach to test this type of application?

Here is one possible solution: the DateFactory class , which provides some methods for generating the predicted date during testing and the real-time value during production. Do you have any thoughts on this approach or have you tried to do something else in practice?

+3
source share
2 answers

In the link you cited, the author somewhat rejects the idea of ​​adding additional parameters to your methods for unit testing, but in some cases, I think that you can justify this as simply expanding your business logic. In my opinion, this is a form of control inversion that can make your model more flexible and, perhaps, even more expressive. For example:

def is_expired(self, check_date=None):
    _check_date = check_date or datetime.utcnow()
    return self.create_date + timedelta(days=15) < _check_date

Essentially, this allows my unit test to provide its own date / time for checking my logic.

, , , API. , , / . , .

+3

, , ( ). DayFactory, , - , .

Python Datetime.now Time.now . , . , ( ) , .

   def setUp(self) 
      self.oldNow = Datetime.now
      Datetime.now = self._fakenow
      ...

   def tearDown(self)
      Datetime.now = self.oldNow

, , .

DateFactory , , tearDown.

+1

Source: https://habr.com/ru/post/1706679/


All Articles