Write cross-platform unit tests that handle assertions on line endings

Write cross-platform unit tests that handle assertions on line endings

what is unit testing with example
how to write unit tests c#
c# unit test framework
unit testing tools
unit testing c# best practices
unit testing framework
how to do unit testing
unit testing documentation

I'm using JUnit to test the printed output of a program. I'm doing this by setting System.out to my own PrintStream which prints to a ByteArrayOutputStream. My tests usually look like this:

assertEquals("Some line of text\r\n", output);

I'm interested in how I might make this code work across platforms. If I run these tests on linux, all these tests will fail. I could write more complicated tests, which accept either \r\n or \n in the output, but I'd like to keep the tests simple if possible. Is there a way to set the type of newline printed by println()?


You should keep the system's default behavior, and change your assert code:

String EOL = System.getProperty("line.separator")
assertEquals("Some line of text" + EOL, output);

Why? The EOL (end-of-line, or line-separator) is system-dependent, if you change the default behavior, you MAY meet other strange bugs.

In your case, it is enough for you to change the EOL in your own string.

Other (maybe not good) methods may include: replace all \r\n and \r to \n and use \n to compare all outputs, not use PrintStream but build the string by yourself, etc.

Unit Testing Tutorial: 6 Best Practices to Get Up To Speed, And yet, a form of peer pressure causes them to play that close to the vest. Not every test you could conceivably write qualifies as a unit test. the command line and test for output, you're executing an end-to-end But the creators of the term left the designation deliberately vague, presumably to cross  The test runner. With the unit and unit test in hand, we now need a test runner that knows how to execute Jasmine tests and report results. For this first exercise, we’ll run tests on the command line using Karma, which you’ll need to install along with Jasmine as follows: Make sure you have Node.js installed.


When I write tests that need to handle multi line output, I read the output using a BufferedReader, and insert the lines into a list. Then I can assert the contents of the list line by line.

That way I don’t care what platform-specific chars the output uses for newlines because the reader is taking care of it and I’m comparing only the individual lines.

11 Best Unit Testing Frameworks For Selenium Automation , Deciding which unit testing framework would be best suited for your Selenium test scripts? Here are the top 11 unit testing frameworks for Selenium automation. With features like assert writing, PyTest has become the first choice of It is a stable and cross-platform programming language created by  The writing and reporting of assertions in tests¶ Asserting with the assert statement ¶ pytest allows you to use the standard python assert for verifying expectations and values in Python tests.


For JUnit 4 there is the library System Rules that helps with such kind of tests. It supports normalization of line endings, too. A test would look like

public class MyTest {
  @Rule
  public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

  @Test
  public void writesTextToSystemOut() {
    System.out.print(String.format("Some line of text%n)");
    assertEquals(
      "Some line of text\n",
      systemOutRule.getLogWithNormalizedLineSeparator()
    );
  }
}

Full Disclosure: I'm the author of System Rules.

Most Important & Widely Used Top 20 Unit Testing Tools, This article mainly discusses Unit Testing Tools but briefly not thoroughly, Developers can use these tools and API to write test using TestNG or JUnit This tool provides 3 types of code coverage such as Line Coverage, Path The quilt is a free cross-platform based software utility and Java software  Asserting Equality in your C# unit tests. Allowing you to have clearer and more concise test assertions, also helping you to comply with the one assertion per unit test best practice.


Perfecting custom assertions in Swift - Alex Curran, Unit testing is a powerful tool, and assertions a key part. The downside with testing is that it is very easy to write a test that ends up being a nightmare to read! In the case above, the line XCTAssertEqual(result, 4) is the assertion to put the red cross in Xcode, and where to show the error message. Not every test you could conceivably write qualifies as a unit test. If you write code that stuffs things into a database or that reads a file from disk, you have not written a unit test. Unit tests don’t deal with their environment and with external systems to the codebase.


Unit Testing with JUnit - Tutorial, This tutorial explains unit testing with JUnit 4.x and JUnit5. You use an assert method, provided by JUnit or another assert framework, to check This class executes your test class and write potential failures to the console. This class can be executed like any other Java program on the command line. It is considered an anti-pattern to have multiple asserts in a single unit test. A single unit test is expected to test only one thing. Perhaps you are testing too much. Consider splitting this test up into multiple tests. This way you can name each test properly. Sometimes however, it is okay to check multiple things at the same time.


Unit testing, In computer programming, unit testing is a software testing method by which individual units of Writing and maintaining unit tests can be made faster by using parameterized tests. These allow the execution Bugs in released code may also cause costly problems for the end-users of the software. Code can be impossible  The Perforce line-end options can be used to convert your text file line endings regardless of the platform where your client workspace resides. For example, a Mac Classic user can set their client workspace line-end option to win , to sync text files to their workspace and retain Windows-style CR/LF line-endings.