question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Abort tests if URL is unreachable

See original GitHub issue

Imagine having a suite of integration-y tests that use some service that’s fickle and may be down occasionally and that you’re ok with not running the tests if it isn’t reachable.

Enter DisabledIfUnreachable extension!

@DisabledIfUnreachable(url = "some.url", timeoutMillis = 5_000)
class FickleServiceRelatedTests {

	void testA() {
		// something with the service
	}

	@DisabledIfUnreachable(url = "some.other.url", timeoutMillis = 5_000)
	void testB() {
		// something else with the service
	}

}

Test A is disabled if some.url isn’t reachable (i.e. times out in 5 seconds or returns a status code outside of 200-399). Same for B but it is also disabled if some.other.url isn’t reachable.

I’ve started playing with this idea a few years ago and have just put the code I wrote into this gist (which should be seen as experimental and not up to our quality standards; also it uses EnabledIf wording, but I don’t think that’s a good idea).

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Michael1993commented, May 3, 2021

Hm, I’m not a 100% sure that an annotation is the proper place for what is essentially an assumption.

We should consider providing this functionality in the form of a utility class (e.g. PioneerAssumptions) with an assumeUrlIsReachable(String url, int timeoutMillis)

0reactions
Bukamacommented, May 5, 2021

Thinking this over, I realized that I couldn’t draw a clear boundary between uses cases for conditions (i.e. disabling a test) and for assumptions (i.e. aborting it). JUnit says:

Assumptions are typically used whenever it does not make sense to continue execution of a given test method — for example, if the test depends on something that does not exist in the current runtime environment.

That strongly suggests that this should indeed be an assumption. (I’m not 100% sure whether JDK versions and OSes shouldn’t be assumption as well [or this!], then, but that’s beside the point.) I’ll update the issue accordingly.

I’m totally with you: This cleary is an assumption.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Check if a Website is Up or Down [5 Ways] - Geekflare
Simply enter the URL or name of the website you would like to test and click “Check”. Compared to other simple ping sites,...
Read more >
Set Up and Tear Down State in Your Tests - Apple Developer
When you run a test case, XCTest calls the XCTestCase setUp() class method first. Use this method to set up state common to...
Read more >
How can I simulate a 'down' website? - Stack Overflow
However, I cannot seem to figure out how to simulate a test if a website is 'down' or not. If I try an...
Read more >
Is This Website Down? How to Check if Your Site is Working ...
Here are the steps to check your website online status using this test tool: Visit Site24x7, and you will instantly see a field...
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
(If you are already familiar with the basic concepts of testing, you might want to skip to the list of ... Stop the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found