Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
What's the point of running unit tests in random order?

I noticed the default behavior of Karma for my Angular builds is to run the Jasmine unit tests in random order. What is the benefit of running your tests in random order, vs running them in the same order every time?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Sometimes tests modify state that doesn't get reset for every test run. Maybe a test modifies a global variable that is shared by all tests. Maybe a test writew something to the database and doesn't clean it up when the test is done. These things shouldn't happen, but sometimes they do happen.

For example:

// Stupid contrived example. Don't ever do this.

let num = 0

it('A', () => {
  expect(num + 1).toEqual(1)
})

it('B', () => {
  num = 10
  expect(num + 2).toEqual(12)
})

it('C', () => {
  expect(num + 3).toEqual(13) // passes if B is run before C, otherwise fails
})

This may work fine if you always run test A, then B, then C. But then strange things my happen if you run only test C. Because of the changes that test A and B made, C passes when you run them all together. But then you run test C alone and it fails.

And now you are staring at you terminal output dumbfounded muttering "how on earth can test C fail?! It just passed!"

Randomizing the order helps with this a bit. Each test should be totally isolated and it shouldn't matter what order they run in. Randomizing them may help uncover this badness.

So, given that the order shouldn't matter, then why not randomize the order?

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda