Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 271243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:07:50+00:00 2026-05-12T00:07:50+00:00

I’m a newbie to Unit Testing and I’m after some best practice advice. I’m

  • 0

I’m a newbie to Unit Testing and I’m after some best practice advice. I’m coding in Cocoa using Xcode.

I’ve got a method that’s validating a URL that a user enters. I want it to only accept http:// protocol and only accept URLs that have valid characters.

Is it acceptable to have one test for this and use a test data file? The data file provides example valid/invalid URLs and whether or not the URL should validate. I’m also using this to check the description and domain of the error message.

Why I’m doing this

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

But on the other hand…

If I’m testing for:

  • invalid characters
  • and an invalid protocol
  • and valid URLs

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

Is what I’m doing OK?

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

  • 1 1 Answer
  • 5 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:07:51+00:00Added an answer on May 12, 2026 at 12:07 am

    In general, use a test data file only when it’s necessary. There are a number of disadvantages to using a test data file:

    • The code for your test is split between the test code and the test data file. This makes the test more difficult to understand and maintain.
    • You want to keep your unit tests as fast as possible. Having tests that unnecessarily read data files can slow down your tests.

    There are a few cases where I do use data files:

    • The input is large (for example, an XML document). While you could use String concatenation to create a large input, it can make the test code hard to read.
    • The test is actually testing code that reads a file. Even in this case, you might want to have the test write a sample file in a temporary directory so that all of the code for the test is in one place.

    Instead of encoding the valid and invalid URLs in the file, I suggest writing the tests in code. I suggest creating a test for invalid characters, a test for invalid protocol(s), a test for invalid domain(s), and a test for a valid URL. If you don’t think that has enough coverage, you can create a mini integration test to test multiple valid and invalid URLs. Here’s an example in Java and 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.