Unable to create unit test for module - Does this mean poor design?

I have an application that returns data depending on the specified time, I can specify days, months or years. The problem is that if I were to start the application today and ask him to return data from 1 month ago, and after 3 months I should ask the application to return data from this date to the previous 1 month (i.e. 1 month from the date ) the results will obviously be different. Due to the dynamic nature of this, it is difficult for me to create unit tests because I have to change the date depending on when I run the tests. Does this mean poor design or is it an exception?

+3
source share
11 answers

What you do is not a unit test . Unit tests should use a small “block” of your code and only your code. By turning on system time, you are also testing the environment in which you are currently working. This is a system test . Unit tests are very effective tools to make sure your code is written correctly, but it helps many of you write your code in a “verifiable” way.

, , , . , , "" , " stub" " mock objects" .

, , - , . . , , , . , - . ( Google Testing , unit test , .) - , , , , .. , .

. , . , , . 2 3 " " . Python , OO-:

class MyClass(object):
    def _get_current_time(self):
        '''This is a test seam'''
        return datetime.datetime.now()

    def age(self):
        return self._get_current_time() - self._birthday

:

class FakeMyClass(MyClass):
    def __init__(self, test_time, *args, **kwargs):
        self._test_time = test_time
        MyClass.__init__(self, *args, **kwargs)

    def _get_current_time(self)
        return self._test_time

, FakeMyClass, , :

myclass = FakeMyClass(t)
self.assertEqual(myclass.age(), expected_age)

, , .

+1

, , .

1 , , .

+5

, .

. , .

( , , , , , , , " ", .)

+2

, .

+2

, , , , . , , . , , .

+1

. , , . , .

, .

0

, .

  • , , ?
  • , ?
  • , ?

# 1, , , . H2. .

# 2 # 3, . , .

, QueryRunner, SQL, , , . , QueryRunner , SQL, , . , JDBC.

0

, , "" . ? , , unit test , , ...

, , "" , / , , , , "". , , .

0

, , . / , , "" . unit test , . , , , , . Calendar - .

, , , . , , , , , . : , , . . , , , , ( , , . , , .

( ), Container Injection Dependency. , IoC, , "" - .

0

, ?

: - , .

, . , , .

Visual Studio, datasource , .

0

, . :

1) / .

2) , (, db = now()).

3) Run the query on a clean system, then apply your changes and run the query again. This is probably the easiest way.

-1
source

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


All Articles