General Tips

— Alex Reinhart

My tests failed, what do I do? #

If our unit-testing robot posted in your pull request that your tests failed, it’s time to investigate.

First, run your tests on your computer. Run them on the command line – the comment at the top of the test file tells you the command to do this.

If the tests fail on your computer, you need to fix your code so they pass. Review the logs to find out which tests failed specifically, and investigate what went wrong. Fix the bugs, commit your changes, and push. Our server will run the tests again automatically.

For Python, you can also run your tests in the debugger. This lets you see exactly what happens when a test fails. The command pytest --pdb test_anagrams.py will run the tests in test_anagrams.py and, if one fails, automatically open the Python debugger, which lets you print out the values of variables and see what happened.

If the tests pass on your computer but not on our server, there are several options:

  • If you submitted Python code but the tests failed on the R step, or you submitted R code and the tests failed on the Python step, you started the assignment before doing the mandatory update instructions to fix a bug in our testing system. Follow these instructions before starting your next homework.
  • If the tests failed with the message “Automatic merge failed; fix conflicts and then commit the result”, then you have accidentally committed files from another assignment onto this homework branch. Look at the “Files changed” tab in your pull request and consult at office hours for help fixing this.
  • You used a package we don’t have installed. We’ll take a look and consider installing it. You don’t need to take any action.
  • You hard-coded the path to a file, e.g. by writing open("/Users/yourusername/s750/assignments-yourusername/some-file.txt"). That path does not exist on your server. See the section below on hard-coding paths.
  • You did not place your code in the directory provided by new-homework (e.g. you did not put your code for cow-proximity inside the cow-proximity/ directory), so our test system could not find your tests. Move your files, commit the changes, and push again. Our server will run the tests again automatically.
  • You did not use the new-homework command to start the assignment, and your branch name is not the name of the assignment. The system got confused and couldn’t find your tests. Use new-homework as described in the instructions.

Don’t hard-code paths #

Often your code will need to load specific files, such as through source(), read.csv, or open. It is tempting to write code like this:

cows <- read.table("/Users/yourusername/School/s750/assignments-yourusername/cow-proximity/Data/cows.txt")

print(cow_proximity(cows,  K))

Or like this:

setwd("/Users/yourusername/School/s750/assignments-yourusername/maze-solver/")

read_maze("Data/maze1.txt")

But don’t do this!

Code should be portable: it should be possible for someone else to take your repository and run your code. But they won’t place it in /Users/yourusername/School/s750; they may place it somewhere entirely different.

Instead, use relative paths. Don’t use setwd to hard-code a working directory. Just use relative paths, such as Data/cows.txt; anyone who runs your code from inside the cow-proximity/ directory will find that it works correctly and finds the file, regardless of where they have placed the cow-proximity/ directory.

If you have problems running your code inside RStudio or another editor because it uses the wrong working directory, there is usually an option to set the working directory to where your code currently is. RStudio, for example, has the Session -> Set Working Directory -> To Source File Location button. If you run your code from the command line, you just need to cd to the right place first.