New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On/Off Switch for Fuzzyness #175

Closed
NiklasMM opened this Issue Nov 13, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@NiklasMM

NiklasMM commented Nov 13, 2014

I would propose to support some sort of global mechanism that switches fuzzyness on and off.
The use case is the following: Use deterministic data in unit tests and perform fuzzy testing with fuzzy data while using the same factories.

The easiest solution (which I am using now) is to use a deterministic seed for the random module. Since all FuzzyAttributes use the random module this effectively deactivates the randomness.

I would be interested in other opinions, weather this is good practice or if I am missing something here.

Furthermore, I would propose to bake this into factory_boy. I think it should be possible to use a private random number generator in factory_boy, by using random.getstate() and random.setstate(). This way the state of the "fuzzyness switch" would be independent from the global random generator state, which makes things easier, if the rest of the application has other assumptions on it.

Would you be interested in something like this? I am willing to put in some work to implement this.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Nov 30, 2014

Member

Hi,

The idea looks interesting, although I'm unsure about its execution.
Basically, I try to avoid global state as much as possible — and a global "un-fuzzy" switch would be global.

On the other hand, being able to go "random yet repeatable" sounds like a great feature — if only to reproduce bugs spotted in CI.

How would the user set the random seed in your proposal? Through an environment variable, a call to some factory.fuzzy.reset_random(42), or something else?

Member

rbarrois commented Nov 30, 2014

Hi,

The idea looks interesting, although I'm unsure about its execution.
Basically, I try to avoid global state as much as possible — and a global "un-fuzzy" switch would be global.

On the other hand, being able to go "random yet repeatable" sounds like a great feature — if only to reproduce bugs spotted in CI.

How would the user set the random seed in your proposal? Through an environment variable, a call to some factory.fuzzy.reset_random(42), or something else?

@rbarrois rbarrois added the Feature label Nov 30, 2014

@NiklasMM

This comment has been minimized.

Show comment
Hide comment
@NiklasMM

NiklasMM Dec 1, 2014

How would the user set the random seed in your proposal? Through an environment variable, a call to > some factory.fuzzy.reset_random(42), or something else?

Yes. I thought something along those lines. You are correct of course about the global state. I believe you could argue, however, that most of the arguments on why global state is bad (some are listed here) do not necessarily apply to a testing environment. The "fuzzyness" switch is a "real" global value, like an environment variable (run tests in deterministic or non-deterministic environment). If you want the behaviour I described, where the same code can be used in both cases, I think then, there is no other way. (Please correct me, if I'm wrong 😄 )

If we really want to avoid global state, the only thing I can think of, is something like this:

field_name = factory.fuzzy.FuzzyText(deterministic=True)

In this case you have to explicitly have this in every field in your whole test suite, which is easily forgettable. And you'd still have to set the value through some global variable or another.

NiklasMM commented Dec 1, 2014

How would the user set the random seed in your proposal? Through an environment variable, a call to > some factory.fuzzy.reset_random(42), or something else?

Yes. I thought something along those lines. You are correct of course about the global state. I believe you could argue, however, that most of the arguments on why global state is bad (some are listed here) do not necessarily apply to a testing environment. The "fuzzyness" switch is a "real" global value, like an environment variable (run tests in deterministic or non-deterministic environment). If you want the behaviour I described, where the same code can be used in both cases, I think then, there is no other way. (Please correct me, if I'm wrong 😄 )

If we really want to avoid global state, the only thing I can think of, is something like this:

field_name = factory.fuzzy.FuzzyText(deterministic=True)

In this case you have to explicitly have this in every field in your whole test suite, which is easily forgettable. And you'd still have to set the value through some global variable or another.

@rbarrois rbarrois closed this in 97a8890 Feb 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment