When to choose system test over integration test Rails 5.1?
Asked Answered
G

2

29

With the release of Rails 5.1, they included system tests. Which means we can test our JavaScript too in Rails. I see Rails guide explains a sample test creating article in both ways: via system test and via integration test.

Now the question is: before Rails 5.1 I was writing complex test cases in integration tests. But now I have two options to write a test case. I can write test case like

test: should create article

in integration test, but I can also write the same test case in system test.

So when should I choose system test to write a test case and when to choose integration tests ?

Graphology answered 22/6, 2017 at 5:22 Comment(2)
Integration tests don't use browser, so you can't test js in them.Slipknot
It isn't about picking the best type of tests (integration OR system) but instead using both types together (integration AND system) by testing behavior in integration tests and user interaction in system tests. For example, if you want to test that a piece of data is loaded with ajax on user's click (an interaction) go with System. And go with Integration tests to test that the returned data is what you expected. Or just pick the one that suits you best if you don't want to test both interaction and behavior.Pinchcock
I
31

The short answer has been given by MikDiet. For the long answer, check out the documentation on system and integration tests.

System tests allow for running tests in either a real browser or a headless driver for testing full user interactions with your application.

A quick rule of thumb is that all tests interacting with Javascript need to be run as system tests. But you could also use them to test your responsive layout, since you can specify the screen size of the browser.

Integration tests are used to test how various parts of your application interact. They are generally used to test important workflows within our application.

Integrations tests are different because they are not run through the browser. They still allow you to interact with the HTML of the result page, but remember that it's static output you work with.

In integration tests, you are mostly looking at the behavior of the controller actions and not so much on what the user sees and interacts with. This part of the documentation might help you understand what integration tests are all about: Functional Tests for Your Controllers.

Intention answered 22/6, 2017 at 12:1 Comment(2)
So system tests are an improvement on and replacement for feature tests?Curie
I wouldn't say that. System tests are more or less Minitest's equivalent to RSpec's feature specs. On a high level, they are pretty much the same. On a low level, system tests are a little bit easier to set up and maybe a little bit faster right now, since they are tightly integrated with Rails. But as far as I'm aware, RSpec is planning to use the system test's API for their feature specs, which would make them the exactly the same, just with different syntax.Intention
D
12

TL;DR: I would go with system tests instead of integration tests in any app I started today. The only advantage of integration tests is speed.

I think system tests have 2 great advantages over integration tests:

  • They test interactions with real screens, instead of making synthetic requests for simulating those.
  • They are much more realistic and comprehensive. For example, a broken piece of Javascript will make the spec fail, while it will be ignored in an integration test.

I think the only benefit of integration tests is speed. They are much faster indeed (check this experiment I made). For me, the speed difference is not such a big concern because:

  • I can run isolated system tests in under 2s in my box. That is fast-enough feedback for my coding happiness.
  • I rely on cloud test runners with parallelization for large suites.

I think both local and cloud speed and parallelization are good enough today, and will only get better with time. So I believe system tests is a much safer bet if you are starting a new app today.

Dun answered 20/5, 2018 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.