juan_gandhi: (Default)
[personal profile] juan_gandhi
 So, if you think you call a function in your code, and this function returns current time, or a random number... IT'S NOT A FUNCTION. Your code is function of "random number", or "time".

So, if your code is written as something that retrieves this kind of data, to test your code, you should provide that data. Not just today, but try the time, like 10 years from now. As to "random", You provide the randomness. If your code cannot be fixed to behave as a function of those inputs, make your "random stream" or "time stream" not hard-coded, but substitutable. Mockable. And mock it in your tests. MAKE SURE that you don't provide just happy-path data. Provide anything. A sequence of 100 numbers 4 for random. Time that is 10 years from now. Or even 30 yeas from now.

Make sure that your tests don't depend on anything. Because test Must Be Reproducible.

All these things, I know, are obvious to some, and not obvious to others.

If you still have questions, ask. But don't argue. Because what I say is math. Unless you have another math (some people do), or another logic (there's plenty of them), please don't argue.

I'd be glad to see how all this changes if logic is e.g. linear. 

 

Date: 2020-08-24 05:07 am (UTC)
perdakot: (Default)
From: [personal profile] perdakot
Диск и сеть туда же.

Date: 2020-08-24 08:43 am (UTC)
From: [personal profile] a2is
Интерактивные тесты - туда же :)

Date: 2020-08-24 05:59 am (UTC)
From: [personal profile] mikkim08
У меня было такое задание на интервью. Написать довольно простой вебсервис, который зависит от времени, и юнит-тест для него.

Date: 2020-08-24 09:09 am (UTC)
From: [personal profile] mikkim08
Да нет, мне в целом интервью понравилось :)) Мне дали лаптоп, чтобы на нём программировать, интервьюер сидел рядом и очень доброжелательно помогал. Типа, pair-programming.

Сначала мы быстро-быстро написали простенький сервис (ипользуя jetty), а потом писали тесты, для чего нужно было рефакторить код. При этом пришлось выковыривать из бизнес-логики системный таймер и явно передавать его извне. А потом для тестов этот таймер "замочить" (от слова mock).

Ушло на это всё немногим больше часа.

What to test in time-dependent function?

Date: 2020-08-24 06:01 pm (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)
From: [personal profile] dennisgorelik
> потом писали тесты, для чего нужно было рефакторить код

Что именно вы тестировали, что требовало рефакторинг?

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 07:08 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 08:01 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 08:24 pm (UTC) - Expand

"Should provide that data"?

From: [personal profile] dennisgorelik - Date: 2020-08-24 10:03 pm (UTC) - Expand

Nondeterministic function

From: [personal profile] dennisgorelik - Date: 2020-08-24 11:34 pm (UTC) - Expand

Re: Nondeterministic function

From: [personal profile] dennisgorelik - Date: 2020-08-25 12:08 am (UTC) - Expand

Re: Nondeterministic function

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:26 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 08:33 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 08:41 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 08:49 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 08:59 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 09:06 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 09:28 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 09:36 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 09:49 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 10:01 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 10:10 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-24 10:44 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-24 11:22 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-25 05:30 am (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:31 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-25 03:36 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:48 pm (UTC) - Expand

Re: What to test in time-dependent function?

From: [personal profile] mikkim08 - Date: 2020-08-25 08:09 pm (UTC) - Expand

Multiple separate places to modify code

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:36 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] mikkim08 - Date: 2020-08-25 03:38 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:50 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] mikkim08 - Date: 2020-08-25 08:12 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] dennisgorelik - Date: 2020-08-25 08:21 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] mikkim08 - Date: 2020-08-26 08:02 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] dennisgorelik - Date: 2020-08-26 08:09 pm (UTC) - Expand

Re: Multiple separate places to modify code

From: [personal profile] mikkim08 - Date: 2020-08-26 08:12 pm (UTC) - Expand

Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-26 08:20 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] mikkim08 - Date: 2020-08-27 08:11 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-27 11:35 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-28 12:19 am (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-28 08:48 am (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] mikkim08 - Date: 2020-08-28 07:57 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-29 12:52 am (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] mikkim08 - Date: 2020-08-29 08:33 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] dennisgorelik - Date: 2020-08-29 09:52 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] mikkim08 - Date: 2020-08-30 05:12 pm (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] sassa_nf - Date: 2020-08-29 08:12 am (UTC) - Expand

Re: Tests self-diagnostic

From: [personal profile] mikkim08 - Date: 2020-08-29 08:35 pm (UTC) - Expand

Признаки непонимания

From: [personal profile] dennisgorelik - Date: 2020-08-26 11:37 pm (UTC) - Expand

Re: Признаки непонимания

From: [personal profile] dennisgorelik - Date: 2020-08-27 04:56 pm (UTC) - Expand

Re: Признаки непонимания

From: [personal profile] dennisgorelik - Date: 2020-08-27 11:29 pm (UTC) - Expand

Re: Признаки непонимания

From: [personal profile] dennisgorelik - Date: 2020-08-28 12:21 am (UTC) - Expand

Re: Признаки непонимания

From: [personal profile] dennisgorelik - Date: 2020-08-28 08:30 am (UTC) - Expand

Date: 2020-08-24 06:50 am (UTC)
spamsink: (Default)
From: [personal profile] spamsink
Or even 30 yeas from now.

Remember, remember the 19th of January 2038.

Date: 2020-08-24 08:10 am (UTC)
From: [personal profile] sassa_nf
> A sequence of 100 numbers 4 for random.

I am not sure I understand what one could do about it. How do you defend against this? Write a piece of code for validating the randoms provided are sufficiently random?

It makes more sense to write tests supposing the entropy is depleted. (and the rng device blocks indefinitely)

Date: 2020-08-24 04:46 pm (UTC)
From: [personal profile] sassa_nf
I think I get your point now. We shouldn't mock the entire RNG, but we do need to keep track what seed was used for a given test, so you can re-run the test, if it is seen to fail.

Re: blocking indefinitely - I was thinking of /dev/random. We can deplete it, and we have seen what happens to the programs that don't think that's possible.

Date: 2020-08-24 08:28 am (UTC)
norian: (Default)
From: [personal profile] norian
рандомность в тестах часто обрабатывается записью какой-то достаточно длинной (для создания субъективной иллюзии) последовательности и канонического, прочеканого лапками, вывода, ну то есть регрешн

время есть область ответственности оси, прикладной софт не должно парить что где там переполнилось вообще

Date: 2020-08-24 02:45 pm (UTC)
norian: (Default)
From: [personal profile] norian
это когда пишецца простой логгер, который вкрячиваецца в реальный источнег рандомной цыфири и потом его изображает, ну и конечно можно добавить туда чего угодно лапгами при желании

How to test nondeterministic functions

Date: 2020-08-24 05:52 pm (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)
From: [personal profile] dennisgorelik
> if you think you call a function in your code, and this function returns current time, or a random number... IT'S NOT A FUNCTION.

What you describe as "FUNCTION", is usually called "deterministic function".
~~~~~~~~~~
https://www.google.com/search?q=deterministic+function
Deterministic functions always return the same result any time they are called with a specific set of input values and given the same state of the database.
~~~~~~~~~~
Why did you chose using non-standard "function" label for "deterministic function" concept?

> So, if your code is written as something that retrieves this kind of data, to test your code, you should provide that data.

Not necessarily.
It depends on what exactly we are testing.
For example, if it is sufficient for us to test that nondeterministic function output contains specific phrase that does not depend on random input -- why should we provide random data to that test?

> Make sure that your tests don't depend on anything.

Test that does not depend on anything - does not test production code.
Do you use non-standard definition of word "anything" or phrase "don't depend"?

Edited Date: 2020-08-24 05:54 pm (UTC)

Re: How to test nondeterministic functions

From: [personal profile] dennisgorelik - Date: 2020-08-24 09:39 pm (UTC) - Expand

"Idiots" sign

From: [personal profile] dennisgorelik - Date: 2020-08-24 11:24 pm (UTC) - Expand

Re: "Idiots" sign

From: [personal profile] dennisgorelik - Date: 2020-08-24 11:49 pm (UTC) - Expand

Sings of understanding

From: [personal profile] dennisgorelik - Date: 2020-08-25 03:24 pm (UTC) - Expand

Re: Sings of understanding

From: [personal profile] dennisgorelik - Date: 2020-08-25 05:13 pm (UTC) - Expand

The book

From: [personal profile] dennisgorelik - Date: 2020-08-25 05:38 pm (UTC) - Expand

Re: The book

From: [personal profile] dennisgorelik - Date: 2020-08-25 06:47 pm (UTC) - Expand

Re: The book

From: [personal profile] dennisgorelik - Date: 2020-08-25 07:17 pm (UTC) - Expand

Re: How to test nondeterministic functions

Date: 2020-08-25 07:20 am (UTC)
From: [personal profile] sassa_nf
If you actually read what the search results are about, you would see it is about SQL specifically. It is no longer "usually". No one outside SQL community uses that term. Even the second best result - google corrected the term to "deterministic algorithm", because google knows people don't know what they are talking about, and what they really mean is algorithm.

Is "nondeterministic function" a useful concept

Date: 2020-08-25 04:01 pm (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)
From: [personal profile] dennisgorelik
> No one outside SQL community uses that term.

Your observation is incorrect.

https://www.google.com/search?q=%22deterministic+function%22
About 451,000 results

https://www.google.com/search?q=%22deterministic+function%22+-sql
About 524,000 results

Google's search count estimates are a bit weird here, but the main point is that there are plenty of "deterministic function" results with the word "SQL" in it.

> Even the second best result - google corrected the term to "deterministic algorithm"

It looks like you forgot to add quotes around "deterministic algorithm" in your search.

Re: Is "nondeterministic function" a useful concept

From: [personal profile] sassa_nf - Date: 2020-08-25 05:58 pm (UTC) - Expand

Re: Is "nondeterministic function" a useful concept

From: [personal profile] sassa_nf - Date: 2020-08-25 07:10 pm (UTC) - Expand

Re: Is "nondeterministic function" a useful concept

From: [personal profile] sassa_nf - Date: 2020-08-26 10:30 am (UTC) - Expand

Usage of term "deterministic function"

From: [personal profile] dennisgorelik - Date: 2020-08-26 01:04 pm (UTC) - Expand

Sweeping the uncomfortable facts under the carpet

From: [personal profile] sassa_nf - Date: 2020-08-26 01:22 pm (UTC) - Expand

Misunderstanding question

From: [personal profile] dennisgorelik - Date: 2020-08-26 02:48 pm (UTC) - Expand

Re: "то есть" trick

From: [personal profile] sassa_nf - Date: 2020-08-26 03:45 pm (UTC) - Expand

Expecting answers to unasked questions

From: [personal profile] dennisgorelik - Date: 2020-08-26 06:09 pm (UTC) - Expand

Re: Expecting answers to unasked questions

From: [personal profile] sassa_nf - Date: 2020-08-27 06:10 am (UTC) - Expand

Re: Expecting answers to unasked questions

From: [personal profile] dennisgorelik - Date: 2020-08-27 04:59 pm (UTC) - Expand

Re: "то есть" trick

From: [personal profile] dennisgorelik - Date: 2020-08-26 06:16 pm (UTC) - Expand

Infallible teacher

Date: 2020-08-24 05:57 pm (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)
From: [personal profile] dennisgorelik
> But don't argue.

Because you are perfect and cannot make mistakes, right?

Re: Infallible teacher

Date: 2020-08-24 08:12 pm (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)
From: [personal profile] dennisgorelik
> demonstrate your expertise

I demonstrated my expertise.
Unfortunately, today you are unwilling to comprehend it.

What kind of demonstration would convince you?
Edited Date: 2020-08-24 08:15 pm (UTC)

Re: Infallible teacher

From: [personal profile] dennisgorelik - Date: 2020-08-24 09:55 pm (UTC) - Expand

Date: 2020-08-28 09:27 am (UTC)
From: [identity profile] http://users.livejournal.com/sorcerer-/
Мне не очень понятно почему оно не happy path?
Мне очевидно, что любое конечное количество моков - это happy path при бесконечном оригинале.
Ну и так же, что все, что придумает конкретный программист, - это тоже happy path, т.к. он уже это придумал, и значит предусмотрел в коде.
Я считаю, что тесты бывают только двух видов: integration - что реально видит наш юзер и regression - настоящий un-happy path из баг репортов. Все остальное - карго культ.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

June 2025

S M T W T F S
1 234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 5th, 2025 05:06 am
Powered by Dreamwidth Studios