Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #40

Merged
merged 53 commits into from
Mar 30, 2015
Merged

Test #40

merged 53 commits into from
Mar 30, 2015

Conversation

cpsievert
Copy link
Collaborator

This is currently broken, but a step closer to an ideal testing framework.

Most importantly, I want an argument to specify which browser to use for testing.

It might also be nice to eventually export a function like animint::run_test() one day, so to run a test on brand new code, we'd just have to devtools::load_all(); run_test().

@tdhock
Copy link
Owner

tdhock commented Jan 29, 2015

I tried run_tests("firefox") from both /animint and /animint/tests, but neither worked... (same for phantomjs)

> run_tests()
Unknown option '--webdriver=4444'
Couldnt connect to host on http://localhost:4444/wd/hub.
Please ensure a Selenium server is running.Error in queryRD(paste0(serverURL, "/session"), "POST", qdata = toJSON(serverOpts)) (from #40) : 

Error in read.table(text = pid.tab) (from #7) : no lines available in input
In addition: Warning message:
running command 'lsof -i tcp:4848' had status 1 
> run_tests("firefox")
aesthetics : List of 1
 $ showSelected: symbol foo
.
animation : ...
1 character value : ....12
chunks : ......
coord : ....34
facet-coord : 56
facet lines : .7
facet_grid(space="free") : 89abcdefghijk.l...
facet-strips : mn
aes(href) : .opError:    Summary: NoSuchElement
     Detail: An element could not be located on the page using the given search parameters.
     class: org.openqa.selenium.NoSuchElementException
Error in read.table(text = pid.tab) (from #7) : no lines available in input
In addition: Warning message:
running command 'lsof -i tcp:4848' had status 1 
> getwd()
[1] "/home/thocking/R/animint/tests"
> setwd("testthat")
> run_tests("firefox")
Error in file(filename, "r", encoding = encoding) (from #13) : 
  cannot open the connection
In addition: Warning messages:
1: In run_tests("firefox") :
  Make sure your working directory is set to animint's source path
2: In file(filename, "r", encoding = encoding) :
  cannot open file 'testthat/functions.R': No such file or directory
> setwd("~/R/animint")
> run_tests("firefox")
aesthetics : List of 1
 $ showSelected: symbol foo
.
animation : ...
1 character value : ....12
chunks : ......
coord : ....34
facet-coord : 56
facet lines : .7
facet_grid(space="free") : 89abcdefghijk.l...
facet-strips : mn
aes(href) : .opError:    Summary: NoSuchElement
     Detail: An element could not be located on the page using the given search parameters.
     class: org.openqa.selenium.NoSuchElementException
Error in read.table(text = pid.tab) (from #7) : no lines available in input
In addition: Warning message:
running command 'lsof -i tcp:4848' had status 1 
> 

@cpsievert
Copy link
Collaborator Author

@tdhock you'll want to devtools::install_github("yihui/servr")

The newest version seems to break testing on the master branch (different arguments), but the exciting thing for us is that he added native support for spawning a daemonized file server. This should improve our current hack, but it seems to be the root of the remDr$navigate() issues that I elude to above.

Anyway, I started #41, which simply tries to implement the new daemon support. Let's try to get that working first...

@cpsievert
Copy link
Collaborator Author

@tdhock The way we currently run tests (e.g. devtools::load_all(); testthat::test_dir("tests")) won't allow us to take advantage of the new arguments in run_tests(). So, instead, what I'm thinking is that we should move functions in "tests/testthat/functions.R" and "tests/testthat.R" to R/and export run_tests(). That way, we could do devtools::load_all(); run_tests() for phantomjs and devtools::load_all(); run_tests("firefox") for firefox. If you like that idea, I'll do that next.

@tdhock
Copy link
Owner

tdhock commented Jan 31, 2015

sure, that sounds fine to include those functions in the package and export
them.

On Fri, Jan 30, 2015 at 6:48 PM, Carson notifications@github.com wrote:

@tdhock https://github.com/tdhock The way we currently run tests (e.g. devtools::load_all();
testthat::test_dir("tests")) won't allow us to take advantage of the new
arguments in run_tests(). So, instead, what I'm thinking is that we
should move functions in "tests/testthat/functions.R" and
"tests/testthat.R" to R/and export run_tests(). That way, we could do devtools::load_all();
run_tests() for phantomjs and devtools::load_all(); run_tests("firefox")
for firefox. If you like that idea, I'll do that next.


Reply to this email directly or view it on GitHub
#40 (comment).

@cpsievert
Copy link
Collaborator Author

Ah, yes, I usually inject a browser() instead of manually executing code in this case. So the workflow for adding a new test could be:

library(animint)
init_tests("firefox")
run_tests(filter = "new-test")

If tests fail, inject a browser() at the relevant spot in test-new-test.R, then

devtools::load_all()
run_tests(filter = "new-test")

@cpsievert
Copy link
Collaborator Author

And, actually for sake of autocomplete, these probably make more sense:

tests_init()
tests_run()
tests_exit()

@cpsievert
Copy link
Collaborator Author

@tdhock any idea what I can add/edit here so that Sys.which("phantomjs") works?

@tdhock
Copy link
Owner

tdhock commented Mar 27, 2015

which and Sys.which depends on your PATH environment variable which you can get from the shell via

thocking@silene:~$ echo $PATH
/home/thocking/R/datatable-foverlaps/bedtools2/bin:/home/thocking/R/phantomjs-2.0.0/bin:/home/thocking/bin:/home/thocking/.cabal/bin:/home/thocking/R/datatable-foverlaps/bedtools2/bin:/home/thocking/R/phantomjs-2.0.0/bin:/home/thocking/bin:/home/thocking/.cabal/bin:/home/thocking/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
thocking@silene:~$ 

or in R via

> Sys.getenv("PATH")
[1] "/home/thocking/R/datatable-foverlaps/bedtools2/bin:/home/thocking/R/phantomjs-2.0.0/bin:/home/thocking/bin:/home/thocking/.cabal/bin:/home/thocking/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

so maybe you have to add something to the PATH ? it is weird if /usr/local/bin is not on the PATH by default, but you could add it via (before starting R)

thocking@silene:~$ export PATH=/usr/local/bin:$PATH

- sudo rm -rf /usr/local/phantomjs
- curl -L -O https://s3.amazonaws.com/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2
- tar xjf phantomjs-2.0.0-ubuntu-12.04.tar.bz2
- sudo mv phantomjs /usr/local/phantomjs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably put it under /usr/local/bin (/usr/local is probably not on the PATH)

@cpsievert
Copy link
Collaborator Author

Grr. This is frustrating. If I interactively source the testhat.R script, it seems to work, but it doesn't when I do R CMD check.

My current approach is to start an environment that stores testing info (as well as the remote driver) in tests_init(). To make this environment available to functions.R and the actual testing environment, I do the following. I have a feeling that R CMD check doesn't like that last bit...

@tdhock
Copy link
Owner

tdhock commented Mar 27, 2015

why not just store the remDr object in the global environment, as on the
master branch?

On Fri, Mar 27, 2015 at 11:37 AM, Carson notifications@github.com wrote:

Grr. This is frustrating. If I interactively source the testhat.R script,
it seems to work, but it doesn't when I do R CMD check.

My current approach is to start an environment that stores testing info

animint/R/testHelpers.R

Lines 23 to 24 in 35f279b

# initiate environment that will store important testing info
env <- new.env(parent = globalenv())

(as well as the remote driver) in tests_init(). To make this environment
available to functions.R and the actual testing environment, I do the
following

animint/R/testHelpers.R

Lines 80 to 81 in 35f279b

sys.source(file.path(testDir, "functions.R"), envir = envir)
testthat::test_dir(testDir, filter = filter, env = envir)
.
I have a feeling that R CMD check doesn't like that last bit...


Reply to this email directly or view it on GitHub
#40 (comment).

@cpsievert
Copy link
Collaborator Author

That's probably what I'll do next. I suppose it isn't a huge deal in this case, but I was trying to avoid that since (from my understanding) introducing objects into the global workspace is considered poor practice?

@srvanderplas
Copy link
Collaborator

Is it still poor practice if you remove them afterwards?

On Fri, Mar 27, 2015 at 10:46 AM, Carson notifications@github.com wrote:

That's probably what I'll do next. I suppose it isn't a huge deal in this
case, but I was trying to avoid that since (from my understanding)
introducing objects into the global workspace is considered poor practice?


Reply to this email directly or view it on GitHub
#40 (comment).

@tdhock
Copy link
Owner

tdhock commented Mar 27, 2015

In this case I consider it to be good practice to put remDr in the global
environment, since it simplifies the testing framework (we can assume there
is working remDr in any tests that need to control the browser).

On Fri, Mar 27, 2015 at 11:47 AM, Susan VanderPlas <notifications@github.com

wrote:

Is it still poor practice if you remove them afterwards?

On Fri, Mar 27, 2015 at 10:46 AM, Carson notifications@github.com wrote:

That's probably what I'll do next. I suppose it isn't a huge deal in this
case, but I was trying to avoid that since (from my understanding)
introducing objects into the global workspace is considered poor
practice?


Reply to this email directly or view it on GitHub
#40 (comment).


Reply to this email directly or view it on GitHub
#40 (comment).

@cpsievert
Copy link
Collaborator Author

OK, I think this is finally functional. Go ahead and see if you can find any weaknesses @tdhock.

This should make interactive testing a lot less painful. You can even switch back and forth between different browsers easily:

tests_init()
tests_init("firefox") # shuts down phantomjs before starting selenium server
tests_init() # shuts down selenium before starting phantomjs
tests_run()
tests_exit() # shuts down everything (even local file servers)

@cpsievert
Copy link
Collaborator Author

I'll update the wiki (hopefully tonight) and mention some things to be aware of when writing tests.

if (!inherits(e, "try-error")) {
pids <- as.integer(e)
res <- c(res, tools::pskill(pids))
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e could be an try-error message or a character vector of pids, right? I suggest to clarify using tryCatch:

tryCatch({
  pid.lines <- readLines(pid_file(), warn=FALSE)
  pids <- as.integer(pid.lines)
  res <- c(res, tools::pskill(pids))
})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

That approach will display an error message if there is an error, right? I think it makes sense to fail silently in this case.

@tdhock
Copy link
Owner

tdhock commented Mar 30, 2015

This is great work, thanks a lot Carson.

Besides the minor comments above, I think everything is perfect. Merge it when you like +1

@cpsievert
Copy link
Collaborator Author

For posterity, here are a couple things that are still sub-optimal:

(1) Sys.sleep() non-sense that tries to stall certain calls, but may result in error if background processes are not up and running yet.

(2) My hack for running a "daemonized" file servr. servr has a better solution, but it clashes with RSelenium's navigate method.

cpsievert added a commit that referenced this pull request Mar 30, 2015
@cpsievert cpsievert merged commit c0d87b4 into master Mar 30, 2015
@cpsievert cpsievert deleted the test branch March 30, 2015 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants