devoured elysium
devoured elysium

Reputation: 105217

In Unit Tests, how do you handle variable naming/assignment?

Many times in Unit-Testing, we face the problem of having to define variables to be used by our methods where their value is not that important. Sometimes we have to pass in a string, and check that the output is the same string. Or maybe that same string but in caps. Or that instead of getting that string the method we are testing returns an empty string.

In those cases, how do you declare, name and assign those variables?

Considering the context, for example, of a HTML parser that takes as an input an url:

string google = "http://google.com"

I'm telling what the variable stands for, but not why I am using it specifically (i.e., why am I not using yahoo, instead?), in the context of the test.

string someUrl = "http://google.com";

Here I'm saying that it is "some url", so that it probably isn't important for testing purposes. Yet, I had to choose some url.

string someUrl = SomeUrl(); // in this method I just return "http://google.com"

Here I am stating that the url is not important and I hide the variable's value in a method, probably not distracting the reader of the test with unimportant details.

Is my reasoning correct? Is there other ways to approach this problem?

How do you handle this in your unit-tests?

Upvotes: 2

Views: 1306

Answers (4)

Gishu
Gishu

Reputation: 136653

Name the variable such that it communicates intent.

  • If the variable value doesn't matter, I'd name it as anyUrl = ... Also if it doesn't matter I probably wouldn't use a variable either (unless the inline value hurts readability)
  • If the variable value is special for some reason and it is of consequence to the current test, then I'd have the name convey that urlWithHyphens = .... I also try to introduce a local constant with SCREAMING_CAPS (e.g. VALUE_OVER_THRESHOLD_OF_2_MINS) just for emphasis.

As with OO, Good names is half the battle with readability+maintainability.

Upvotes: 2

fejesjoco
fejesjoco

Reputation: 11903

If I only use one URL in a method, then it's var url. If I use more, then it's either var url1, var url2, or var urlGoogle, var urlYahoo. Keep it short, still meaningful, and don't think too much about it.

Upvotes: 1

Goran Jovic
Goran Jovic

Reputation: 9508

You can choose whichever name you like. It doesn't really matter!

So, names like someSomething are perfectly OK and the hardcoded test values are fine too.

The only convention I do follow is that for choosing the value itself. I've often seen developers thinking for minutes, say, what should be the firstName and lastName of the User object. The solution is to simply have a small set of your own default values you always use. For me, if it is a user who is admin, it's definitely Chuck Norris. This saves you time, and it makes your scripts more consistent, but even that is optional. (However, since you chose your URL to be http://www.google.com" it seems that you're doing this already).

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533780

You should name you variables based on what is clearest to you. Unit-tests can be thought of as test scripts, they don't have to be as efficient or elegant as your core release code.

DEVENTER (n) A decision that's very hard to make because so little depends on it, such as which way to walk around a park

-- The Deeper Meaning of Liff by Douglas Adams and John Lloyd.

Upvotes: 2

Related Questions