External unit test data file

I am new to unit testing and I am after a few best practice tips. I am coding in Cocoa using Xcode.

I have a method that checks the URL that the user enters. I want it to accept only the http: // protocol and only accept URLs with valid characters.

Is it permissible to have one test for this and use a test data file? The data file contains an example of valid / invalid URLs and whether the URL should be validated. I also use this to check the description and scope of the error message.

Why am i doing this

I read Pragmatic Unit Testing in Java with JUnit and it gives an example with an external data file, which makes me think that everything is fine. Plus, this means that I don’t need to write a lot of unit tests with very similar code to test different data.

But on the other side...

If I test:

  • Unacceptable symbols
  • and invalid protocol
  • and valid URLs

all in one test data file (and therefore in the same test), will this cause problems later? I read that one test should complete for only one reason.

What am I doing?

How do other people use test data in their unit tests, if at all?

+5
source share
3 answers

, , . :

  • . .
  • , . , , .

, :

  • (, XML-). String , .
  • , . , , .

, URL- , . , , URL. , , - URL-. Java JUnit:

public void testManyValidUrls() {
  UrlValidator validator = new UrlValidator();
  assertValidUrl(validator, "http://foo.com");
  assertValidUrl(validator, "http://foo.com/home");
  // more asserts here
}

private static void assertValidUrl(UrlValidator validator, String url) {
  assertTrue(url + " should be considered valid", validator.isValid(url);
}
+5

, , , . , , , .

(SUT) / URL, , URL . , , . URL-, , , ( URL- ).

, , - , ( ). , .

, , , . , , . , , , , , , , .

, , . , . , . , "".

, . , - / , , ( ). , - .

, , , , -. .

+1

All Articles