0

I have written a series of test_that tests. There is one test_that test which has a side-effect of creating a sqlite3 table. The rest of the tests rely on this sqlite3 table. Is there a way to force this one test to run before any of the other tests do?

andrew
  • 2,524
  • 2
  • 24
  • 36

1 Answers1

1

If you are using test_dir or test_package (otherwise you can just put the tests in the same file after the sqlite test), you can put your test that generates the table in its own file and use naming conventions for execution. For example, inside tests/run.R you could have:

test_file("tests/testthat/myspecialfile.R")   
test_dir("tests/testthat/")   # will run every file with a name starting with `test`
BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • I'm actually just using test(), but I was under the impression that test_that won't necessarily execute tests in the order they are written in a test file? – andrew Apr 29 '14 at 20:25
  • @andrew What gives you that impression? From my personal experience they do run in the order given and I haven't seen documentation indicating otherwise. Have you run into that situation? – BrodieG Apr 29 '14 at 20:28
  • Ok I got it now. I saw that some variables I had set in early tests were not available in later tests and assumed it was because some tests were finishing before others. But it's actually just that test_that sandboxes the tests. thanks! – andrew Apr 29 '14 at 20:59
  • @andrew, yes `test_that` creates a local environment. You can make those variables available to all tests by declaring them outside of `test_that` blocks. – BrodieG Apr 29 '14 at 21:02